Automatic semicolon insertion

from Wikipedia, the free encyclopedia

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 .


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
  • 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 .


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 , continueand breakwith a label , and returnand throwwith a return value. With version 6 of ECMAScript yieldand the arrow notation are added.


Included in the following examples a, b, cand dfor 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 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;

Since 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:

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 + 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 print()and 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.


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

  1. Ecma-262 Edition 5.1, The ECMAScript Language Specification. 7.9: Automatic Semicolon Insertion
  2. Douglas Crockford: The best thing about JavaScript. O'Reilly-Verlag, 2008. ISBN 978-3-89721-876-5 . Pp. 103, 109f., 127f.
  3. inimino: JavaScript Semicolon Insertion: Everything you need to know. Published on May 28, 2010, accessed February 2, 2015.