Automatic semicolon insertion
Automatic semicolon insertion (English for automatic semicolon insertion , shortly ASI ) refers to a behavior of the JavaScript - parser . Most instructions in JavaScript are terminated with a semicolon . In many cases, however, this semicolon does not have to be specified explicitly in the source text , as the parser adds it on its own. The behavior is controversial because on the one hand it makes programming easier by allowing many semicolons to be left out, and on the other hand, in special cases, it can lead to unexpected behavior and thus to programming errors that are difficult to detect .
definition
The automatic semicolon insertion is specified in the ECMAScript standard and therefore applies not only to JavaScript but also to ActionScript . The parser automatically inserts a semicolon in the following cases:
- The parser encounters an unexpected token and that token is
- a closing curly bracket
}
or - it is separated from the previous token by at least one line break .
- a closing curly bracket
- 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.
return rueckgabewert
return
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 .
Details
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 , continue
and break
with a label , and return
and throw
with a return value. With version 6 of ECMAScript yield
and the arrow notation are added.
Examples
Included in the following examples a
, b
, c
and d
for variables , foo()
, bar()
and baz()
for functions , and print()
for a method of numbers.
The normal case for which the automatic semicolon insertion is intended is shown in the following code:
{ foo()
bar() } baz()
This actually contradicts the syntax of ECMAScript, but the automatic insertion of semicolons results in the following correct code:
{ foo()
;bar() ;} baz();
No semicolon is added in the following code:
a = b
+ c
+ d;
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 else
a syntax error. To correct this, the semicolon (or an empty block) would have to be added explicitly. Of course, the if
construction could also be changed in this case to avoid the problem.
The following example shows a problematic behavior of the automatic semicolon insertion :
return
a + b;
Since return
no line break is allowed immediately after , this code is equivalent to:
return;
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:
return a
+ b;
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 + c
in a
stored, then the print()
method on the sum c + d
to 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 c
called as a function with the argument, to c + d
call the method from the return value print()
and b
to 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.
rating
Douglas Crockford describes automatic semicolon insertion as a "terrible design flaw" in JavaScript and, with a few exceptions, recommends always specifying the semicolon explicitly. JSLint and similar style control tools usually warn when a semicolon is automatically inserted.
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.
Individual evidence
- ↑ Ecma-262 Edition 5.1, The ECMAScript Language Specification. 7.9: Automatic Semicolon Insertion
- ↑ Douglas Crockford: The best thing about JavaScript. O'Reilly-Verlag, 2008. ISBN 978-3-89721-876-5 . Pp. 103, 109f., 127f.
- ↑ inimino: JavaScript Semicolon Insertion: Everything you need to know. Published on May 28, 2010, accessed February 2, 2015.