1: Why is the result of foo && baz not 1? Because true is 1.
var foo = 1;
var baz = 2;
foo && baz; // returns 2, which is true
2: There are two pluses in the console.log(foo + +bar);, what’s the meaning of them?
var foo = 1;
var bar = '2';
console.log(foo + +bar);
That’s because the
&&(logical AND) operator returns the value of the last operand it evaluated. Sincefooistrue, it has to evaluatebarto determine the outcome of the expression (it will only betrueifbaris alsotrue).The opposite would happen with the
||(logical OR) operator. In that case, sincefooistrue, the outcome of the expression is known to betruewithout having to evaluatebar, so the value offoowill be returned.Concerning your second question, the unary
+operator allows to convert the string'2'into the number2.