1

As per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

The expr1 || expr2 makes sense to me. It short circuits the expression and returns the first one, and it makes sense. I use it often to select the passed variable vs the default one. For ex.

var default_connection = 'localhost:27017/task_master';

function setdb(connection){
    var conn_str = connection || default_connection;
    //Do something here
}

The && behaviour just beats me and appears very unreadable to me.

expr1 && expr2 - Returns expr1 if it can be converted to false; otherwise, returns expr2.

Can someone please help me understand this and suggest some use cases where the && can be useful.

EDIT: Found it from the suggested original question. This gives me a good enough reason to use it.

&& is sometimes called a guard operator.

variable = indicator && value

it can be used to set the value only if the indicator is truthy.

My question was that, "Can someone please help me understand this and suggest some use cases where the && can be useful.". I don't know which part of this question was hard to understand. I gave an example of how I found || useful, and the reason above gives a good enough reason how && can be useful too. Yes, I am new to JavaScript, and yes, I am not as smart as you all are. But being a little kind will help me get there. Peace!

8
  • where do you see in the code &&? Commented Nov 3, 2015 at 12:11
  • @David Haim: The code pertains to ||. He's asking about && in general. Note he even asks for example use cases of && - clearly, he doesn't know of any. Commented Nov 3, 2015 at 12:12
  • "Why does JavaScript && operator return the second expression?" Because, being a logical AND, in order for the second expression to even be considered the first has to be truthy, otherwise it short-circuits. It's really no different from how || works - when expr1 is falsy then it'll return expr2 just as && does when expr1 is truthy. Commented Nov 3, 2015 at 12:14
  • What part of that statement do you not understand? It seems very clear to me Commented Nov 3, 2015 at 12:14
  • Check this out grauw.nl/blog/entry/510 Commented Nov 3, 2015 at 12:14

3 Answers 3

6

It stops at a false value and returns the last value (i think it's more clear this way). It's very helpful with nested objects. Say you might have an object like this, but prop or nestedProp might not exist:

var a = {
   prop : {
     nestedProp : {
        hello : 'hello'
     }
   }
}

Now, what if we need to check for the string inside the nestedProp

You can't say:

if(a.prop.nestedProp.hello === 'hello')

Because prop and nestedProp might not be defined and if you try to do that check you'd get a TypeError.

So you do this:

if(a.prop &&.prop.nestedProp && a.prop.nestedProp.hello === 'hello')

It'd be the same thing to try assigning the hello prop:

var hello = a.prop && a.prop.nestedProp && a.prop.nestedProp.hello;

If a.prop is undefined it will stop there and return undefined. If it's a truthy value then it will evaluate a.prop.nestedProp and if it's a falsey value then it will return that otherwise it returns a.prop.nestedProp.hello.

Sign up to request clarification or add additional context in comments.

Comments

0

The short circuiting nature is secondary and less important - firstly || stands for logical or while && stands for logical and.

Since both are lazy in nature, they can stop evaluating after the results are cleared, and this is where the secondary behavior comes from. When the first value of or is true, no need to evaluate the second one. When the first value of and is false, there's no need to evaluate the second one.

Comments

0

Let us look at the at the possible outcomes of a && b for all values of a and b.

if a = false b = false then a && b = false which is equal to a
if a = false b = true then a && b = false which is equal to a
if a = true b = false then a && b = false which is equal to b
if a = true b = true then a && b = true which is equal to b

So when a is false, the expression a && b will return false by its nature. So if a if false we only have to return it.

When a is true, the expression a && b will depend only on the value of b, i.e, true if b = true and false if b = false. Which means that on a being true, we only have to return the value of b

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.