1

I noticed in Javascript, I have occasionally seen

var foo = bar && bar.getSomething()

I know it is probably similar to say:

var foo;
if(bar !== undefined) {
   foo = bar.getSomething();
}

But it still makes me confused. Because to me, it is quite similar to a binary operation such as:

0100 & 0101 == 0100;  //true

However, it is quite different on another hand, because for example, if bar is an object, and bar.getSomething() return an integer(saying 5 in the following example), and the code ends up as:

var foo = {baz : qux} && 5;

it obviously makes no sense to me.

So My question is that why in Javascript, people can code in this way? Is this an implementation of the binary operation in JS? What is the name of this coding practice? is there any performance benefit compared to the traditional way? Thanks

AlexLuo
  • 458
  • 1
  • 4
  • 12
  • 2
    `&&` is the logical AND, not bitwise AND. – Felix Kling Sep 09 '17 at 22:00
  • `is there any performance benefit compared to the traditional way?` Most likely not, Javascript Engines are pretty smart these day, micro optimisations like this usually don't gain anything. The main use is just to make code shorter, it's basically saying keep executing from the left, until something returns false. You can chain them more than just 2, eg.. action1 && action2 && action3, if action2 returned false, action3 wouldn't get executed. – Keith Sep 09 '17 at 22:09
  • I'm not actually sure what you want to know. People use code like this because they can and because it's shorted than using an `if` statement. – Felix Kling Sep 09 '17 at 22:09
  • @FelixKling I know people use in this way. But I'm not confident to use it until I know how it works, and why it works. So I ended up to ask this question. – AlexLuo Sep 09 '17 at 22:10
  • So you are basically asking how logical operators work? – Felix Kling Sep 09 '17 at 22:10
  • `var foo = {baz : qux} && 5;` "it obviously makes no sense to me.," Me neither, as it's always going to return 5, as {bas:qux} is an object literal that's true. So were have you seen this code?.. – Keith Sep 09 '17 at 22:14
  • Lots of duplicates ;) – Felix Kling Sep 09 '17 at 22:15
  • @FelixKling, yea, I think I lack some JS knowledge fundamentally. For example, why if(bar) could return true; so, if "if(bar)" returns true, then if(bar && bar.getSth()) returning true seems also make sense, and then bar && bar.getSth() is equvalent to if( bar && bar.getSth()), am I right? – AlexLuo Sep 09 '17 at 22:17
  • No. `bar && bar.getSth()` is the same as `if (bar) { bar.getSth() }`. I.e. the second operand is only evaluated if the first on is truthy. And that's exactly what the `if` statement expresses as well: Execute `bar.getSth()` if and only if `bar` is truthy. – Felix Kling Sep 09 '17 at 22:19

1 Answers1

0

I think you are confused between a & operator and an && operator, the & operator would return the bitwise &, while the && operator returns the the last operand if all values are true or the first falsy value if any of the values is falsy

console.log(4 & 5);
console.log(4 && 5);
console.log(!!(4 && 5));
console.log(0 && 5);
console.log(5 && 0);
console.log(null && 5);
console.log(5 && null);

you can use the !! operator to make sure it is boolean

marvel308
  • 10,288
  • 1
  • 21
  • 32