3

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?

1
  • that looks about right. the second block { console.log("5 < 2"); } is not considered a part of the else at all ... and ( 5 < 2 ) while pointless is valid - it's like else { ( 5<2 ) } Commented Sep 24, 2016 at 7:29

3 Answers 3

6

The difference is that in the second case, the carriage return makes the second braced block a standalone block rather than part of the else clause where the parenthetical part is the else clause. The second case is equivalent to:

if (5 > 2) {
    console.log("5 > 2");
} else {
    (5 < 2)
}

{
    console.log("5 < 2");
}

which is just:

if (5 > 2) {
    console.log("5 > 2");
} else {
    false
}

console.log("5 < 2");
Sign up to request clarification or add additional context in comments.

Comments

3

The reason for the different interpretation is the first of the ECMAScript Rules of Automatic Semicolon Insertion:

  1. When, as the program is parsed from left to right, a token (called the offending token) is encountered that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token if one or more of the following conditions is true:

    • The offending token is separated from the previous token by at least one LineTerminator.
    • The offending token is }.

In your case, the offending token is {. In the first version of your code, neither of the bullet point conditions quoted above is true, so no semicolon is inserted, and a syntax error is raised.

In the second example of your code, there is a line terminator, as mentioned in the first condition. In that case a semicolon is inserted automatically so the parsed code really becomes this:

if (5 > 2) {
  console.log("5 > 2");
} else (5 < 2);
{
  console.log("5 < 2");
}

Now the code is valid as the opening brace that follows the semicolon is interpreted as the start of a code block that is not part of the expression that follows the else.

Note that even though this code now parses, it is clear that the expression in the else part has no effect.

2 Comments

Thank you so much for such a fast and complete answer :) I was simply shocked when the code after automatic beautification resulted in different output.
You're welcome. "Automatic Comma Insertion" is indeed one of the less appreciated syntax rules of JavaScript.
3

It's all because Automatic Semicolon Insertion, when you transfer this brace to next line, JS engine "translates" your code to this:

if (5 > 2) {
  console.log("5 > 2");
} else (5 < 2);
{
  console.log("5 < 2");
}

Which is obviously correct, because JS allows placing stand-alone blocks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.