As far as I know, The logical && works like the following
var test = false;
var foo = test && 42;
This code, will assign 42 to foo only if the first condition gets evaluated to true. So in this example, foo will keep its current value.
I’m wondering why this snippet doesn’t work at all:
var test = "";
var foo = test && 42;
Now, foo gets the value from test assigned. I’m very confused. The empty string is one of Javascripts falsy values, so why would the && operator fail in this scenario ?
Can someone help me out with the spec on this one please ?
You have answered your question yourself.
42will be assigned tofooonly iftestevaluated to true.So if
testis empty string (evaluated to false), 42 won’t assigned tofoo,foowill be the empty string.