This short snippet of code is obviously faulty:
if (5 > 2) {
console.log("5 > 2");
} else (5 < 2) {
console.log("5 < 2");
}
Condition for else should not be specified. Execution of this code will result in SyntaxError: Unexpected token {, as expected.
The problem is the fact that after a minor change (transferring the left brace to the new line), interpreter will simply ignore the syntax error.
This code:
if (5 > 2) {
console.log("5 > 2");
} else (5 < 2)
{
console.log("5 < 2");
}
results in this output (tested in chrome and firefox):
5 > 2
5 < 2
How is this possible? Why this else is not treated as a syntax error?
{ console.log("5 < 2"); }is not considered a part of the else at all ... and( 5 < 2 )while pointless is valid - it's likeelse { ( 5<2 ) }