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

9
  • 2
    && is the logical AND, not bitwise AND. Commented Sep 9, 2017 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. Commented Sep 9, 2017 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. Commented Sep 9, 2017 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. Commented Sep 9, 2017 at 22:10
  • So you are basically asking how logical operators work? Commented Sep 9, 2017 at 22:10

1 Answer 1

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

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

8 Comments

"returns the the last element or false" That's not correct. It returns either the first operand if it is falsy, or the second operand. It only returns false if one of the operands is false.
yeah, I mixed the two. but I'm a little confused, why code in this way, just because it is clean?
@FelixKling it means it will return false if 1 of the elements is falsy, else the last parameter
No, that's wrong. 0 && 5 returns 0, not false.
updated the answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.