Automatic semicolon insertion
- The parser encounters an unexpected token and that token is
- The parser comes to the end of the source text, although the grammar does not allow this.
- The parser encounters a token that is separated from the previous token by a line break, but which would form an instruction with this token within which a line break is explicitly prohibited. This applies, for example, to the fact that no line breaks are allowed between the keyword and the return value.
The semicolon is inserted before the unexpected token or before the end of the source text. A semicolon, which would be automatically inserted according to these rules, is still not inserted if one of the two following cases applies:
- The inserted semicolon forms an empty statement.
- The inserted semicolon becomes one of the two semicolons in the head of a for loop .
In the case of single-line comments , the line break that closes the comment is not part of the comment, so it represents a line break in the sense of the above rules. Multi-line comments are also treated like line breaks. The inserted semicolon comes after the line break, while an explicitly specified semicolon is usually placed at the end of the line; however, this makes no difference to the parser.
The statements in which a line break is prohibited are the post-increment and post-decrement operators ,
breakwith a label , and
throwwith a return value. With version 6 of ECMAScript
yieldand the arrow notation are added.
The normal case for which the automatic semicolon insertion is intended is shown in the following code:
bar() } baz()
This actually contradicts the syntax of ECMAScript, but the automatic insertion of semicolons results in the following correct code:
;bar() ;} baz();
No semicolon is added in the following code:
a = b
The code is valid without adding semicolons to the line break. This allows long lines of code to be spread over several lines.
In some cases, omitting semicolons leads to syntax errors if these are not added as expected:
if (a > b)
else c = d
No semicolon is added after the first line, as this would result in an empty statement. This causes
elsea syntax error. To correct this, the semicolon (or an empty block) would have to be added explicitly. Of course, the
ifconstruction could also be changed in this case to avoid the problem.
The following example shows a problematic behavior of the automatic semicolon insertion :
a + b;
returnno line break is allowed immediately after , this code is equivalent to:
a + b;
Instead of the total
a + b, nothing (
undefined) is returned. This mistake can easily be overlooked. If a long return value is split over several lines, the beginning must be in the same line as
return, the following code would be correct:
A similar unexpected error arises from the following code:
a = b + c
(c + d).print()
Here, first, the sum should actually into two statements
b + cin
astored, then the
print()method on the sum
c + dto be applied. In fact, no semicolon is added between the two lines because the syntax is correct even without this. So the code is treated like:
a = b + c(c + d).print();
So it is tried to be
ccalled as a function with the argument, to
c + dcall the method from the return value
bto add this result in order to save this sum in
a. This will most likely trigger a runtime error , but in any case it will not produce the expected result. A similar problem can arise when a line begins with a regular expression that can be interpreted as a division with the previous line.
On the other hand, there are also programmers who like to use automatic semicolon insertion . To avoid unexpected errors, lines that begin with parentheses or a regular expression can be preceded by a semicolon. This prevents an originally explicitly set semicolon from being in the required position after lines of code have been moved to another position.
- Ecma-262 Edition 5.1, The ECMAScript Language Specification. 7.9: Automatic Semicolon Insertion