Consider the following 3 scenarios.
var y = 1;
if (function f(){}) {
y += typeof f;
}
console.log(y); // "1undefined"
the above output indicates that function f(){}
is just checked for its truthiness, but is not defined before the execution of if block
.
var y = 1;
if (y--) {
y += typeof f;
}
console.log(y); // "0undefined"
However, here we get the value of y as 0, that means the expression inside if condition
is executed before the if block
. But shouldn't the if block
be skipped as y--
evaluates to 0 which is a falsey value as in below.
var y = 1;
if (0) {
y += typeof f;
}
console.log(y); // "1"