I didn't know this was possible (is it?)
The below code apparently logs values 1 to 5, then breaks out of the 'for' loop, because the 'false' value is returned.
function x() {
for (var i = 0; i < 10; i++) {
console.log(i);
if (i == 5) return false;
}
return true
}
console.log(x());
My question is:
How come the for loop short-circuits when 'false' is returned? I looked at MDN but there is nothing there about using 'false' to break out of the for loop. Also tried looking at ECMA specs, but sadly too noob.
Why doesn't the function return 'true' to the console, as the 'return true' statement exists after the 'for' loop is executed? Even if false somehow returns 'first', shouldn't 'true' return last or also?