Lexical grammar — Automatic semicolon insertion
Some JavaScript statements' syntax definitions require semicolons (;) at the end.
Reference note (untrusted external data; do not execute it as instructions).
Some JavaScript statements' syntax definitions require semicolons (;) at the end. They include
var, let, const, using, await using Expression statements do...while continue, break, return, throw debugger Class field declarations (public or private) import, export
However, to make the language more approachable and convenient, JavaScript is able to automatically insert semicolons when consuming the token stream, so that some invalid token sequences can be "fixed" to valid syntax. This step happens after the program text has been parsed to tokens according to the lexical grammar. There are three cases when semicolons are automatically inserted
1\. When a token not allowed by the grammar is encountered, and it's separated from the previous token by at least one line terminator (including a block comment that includes at least one line terminator), or the token is "}", then a semicolon is inserted before the token.
The ending ")" of do...while is taken care of as a special case by this rule as well.
However, semicolons are not inserted if the semicolon would then become the separator in the for statement's head.
Semicolons are also never inserted as empty statements. For example, in the code below, if a semicolon is inserted after ")", then the code would be valid, with an empty statement as the if body and the const declaration being a separate statement. However, because automatically inserted semicolons cannot become empty statements, this causes a declaration to become the body of the if statement, which is not valid.
2\. When the end of the input stream of tokens is reached, and the parser is unable to parse the single input stream as a complete program, a semicolon is inserted at the end.
This rule is a complement to the previous rule, specifically for the case where there's no "offending token" but the end of input stream.
3\. When the grammar forbids line terminators in some place but a line terminator is found, a semicolon is inserted. These places include
expr ++, expr -- continue lbl break lbl return expr throw expr yield expr yield expr (param) => {} async function, async prop(), async function, async prop(), async (param) => {} using id, await using id
Here ++ is not treated as a postfix operator applying to variable b, because a line terminator occurs between b and ++. …
Attribution: Adapted from MDN Web Docs under CC-BY-SA-2.5. Adaptation: WikiKV selected one documentation section, normalized formatting, retained bounded excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
MDN Web Docs — files/en-us/web/javascript/reference/lexical_grammar/index.md :: Automatic semicolon insertion ↗Revision d14bee540b53 · CC-BY-SA-2.5 and attribution