12.10 Interesting Cases of Automatic Semicolon Insertion
ECMAScript programs can be written in a style with very few semicolons by relying on automatic semicolon insertion. As described above, semicolons are not inserted at every newline, and automatic semicolon insertion can depend on multiple tokens across line terminators.
As new syntactic features are added to ECMAScript, additional grammar productions could be added that cause lines relying on automatic semicolon insertion preceding them to change grammar productions when parsed.
For the purposes of this section, a case of automatic semicolon insertion is considered interesting if it is a place where a semicolon may or may not be inserted, depending on the source text which precedes it. The rest of this section describes a number of interesting cases of automatic semicolon insertion in this version of ECMAScript.
12.10.1 Interesting Cases of Automatic Semicolon Insertion in Statement Lists
- An opening parenthesis (
(). Without a semicolon, the two lines together are treated as a
- An opening square bracket (
[). Without a semicolon, the two lines together are treated as property access, rather than an
- A template literal (
`). Without a semicolon, the two lines together are interpreted as a tagged Template (
13.3.11), with the previous expression as the MemberExpression.
-. Without a semicolon, the two lines together are interpreted as a usage of the corresponding binary operator.
- A RegExp literal. Without a semicolon, the two lines together may be parsed instead as the
MultiplicativeOperator, for example if the RegExp has flags.