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!

Community
  • 1
  • 1
Rahul Soni
  • 4,941
  • 3
  • 35
  • 58
  • where do you see in the code `&&`? – David Haim Nov 03 '15 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. – BoltClock Nov 03 '15 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. – BoltClock Nov 03 '15 at 12:14
  • What part of that statement do you not understand? It seems very clear to me – musefan Nov 03 '15 at 12:14
  • Check this out http://www.grauw.nl/blog/entry/510 – Kaushik Nov 03 '15 at 12:14
  • So you understand the short-circuiting behaviour of `||` but not the one of `&&`? – Bergi Nov 03 '15 at 12:15
  • I'm not understanding the OP question if so. he needs examples on when to use `&&`? – David Haim Nov 03 '15 at 12:15
  • @DavidHaim sorry about being unclear earlier. I was indeed looking for examples when to use &&. For || it made a lot of sense. I have edited my question a bit, since I found a good example of using && sideeffect. The logical bit was/is clear to me, and so is the documentation. I am new to JavaScript and need a little support. I will try my best to be a lot clearer in the future. – Rahul Soni Nov 04 '15 at 03:55

3 Answers3

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.

MinusFour
  • 13,913
  • 3
  • 30
  • 39
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.

fdreger
  • 12,264
  • 1
  • 36
  • 42
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

Sinstein
  • 887
  • 11
  • 42