Saturday, July 4, 2015

wacky double ampersand operator in JavaScript

It will return the right value if the left is truthy and the left value if the left is falsey, so in this example:

var four = 4;
var five = 5;
var whatever = four && five;
alert(whatever);

 
 

...it's going to give us a 5. However if you set the four variable to null it will give us null or if you set the four variable to 0 it will give us 0.

No comments:

Post a Comment