JavaScript does not end a statement at every newline. Automatic semicolon insertion (ASI) is a conditional rule in the ECMAScript grammar: it can supply a semicolon in specific situations, but it will not necessarily separate two lines if the second can continue the first expression. The practical rule is to keep restricted tokens with their operands and use an explicit semicolon before a new statement that could be parsed as a continuation.
When does JavaScript insert a semicolon?
The ECMAScript specification says, “Most ECMAScript statements and declarations must be terminated with a semicolon.” It also permits semicolons to be omitted in certain situations through ASI. The rule is defined in ECMAScript 2026, clause 12.10; it is not a formatter that turns every newline into a statement boundary.
As an Amazon Associate I earn from qualifying purchases.
ASI applies in three broad cases:
- Before a token the grammar cannot accept: insertion may occur when that token is preceded by a line terminator, when the token is a closing brace, or when it follows a closing parenthesis that would end a
do…whilestatement. - At the end of input: the parser may insert a semicolon if one is needed for the input to parse as the intended goal.
- Before a restricted token: when a grammar rule marked “no LineTerminator here” is interrupted by a line terminator, ASI inserts a boundary before the restricted token.
These rules work together with the grammar and the token sequence. A newline matters, but by itself it does not guarantee that the preceding statement ends.
Comments and line terminators
A single-line comment does not independently change ASI behavior; the line terminator at the end of the comment is recognized separately. A multiline comment that contains a line terminator contributes a line terminator to the input stream, so code split by such a comment can encounter the same restricted-line behavior as code split by a visible newline.
#1 Best Overall
Which line breaks change meaning or make code invalid?
Some grammar productions prohibit a line terminator at a particular point. In those cases, moving a token to the next line can change the parse or make the program invalid.
return followed by a newline
function getValue() {
return
{ answer: 42 }
}
The line terminator ends the return statement, so this function does not return the object. In this example, the brace begins a separate block. Put the returned expression on the same line as return to express the intended result:
Rank #2
function getValue() {
return { answer: 42 };
}
throw followed by a newline
throw
new Error("failure");
A line terminator is not permitted between throw and its expression. ASI does not make this a valid throw statement; the code is a syntax error. Keep the expression after throw on the same line.
Free tools Windows power users keep installed
One-click scans. No signup required.
Postfix ++ or -- separated from its operand
value
++next;
The line break prevents ++ from attaching as a postfix operator to value. It is parsed as a prefix update expression on next, rather than as an increment of value. Keep a postfix update on the same line as the value it follows.
Other restricted positions
The same general restriction applies in other grammatical contexts, including a label following break or continue, an assignment expression after yield, arrow parameters and =>, and tokens following async in async function and method forms. The exact allowed placement depends on the production, so do not assume that a line break is harmless merely because the code looks visually separate.
When can the next line continue the expression?
A newline does not force a new statement when the next token can validly extend the expression already being parsed. For example:
Rank #4
const result = first + second
(third + fourth).print()
The parenthesized expression can be interpreted as arguments in a call on the result of first + second, rather than as a separate expression. The ECMAScript specification gives the analogous example a = b + c followed by (d + e).print(): it is parsed as a continuation, not two independent statements.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteCommon continuation starters include:
(, which can begin a call;[, which can begin computed property access;- a template literal, which can be tagged by the preceding expression;
- unary
+or-, which can instead act as a binary operator continuing the expression; - a slash-starting expression, which may be parsed as division in context.
If you intend one of these forms to start a new statement, terminate the preceding statement explicitly or restructure the code so the intended parse is unambiguous. This is defensive style, not a requirement to write a semicolon after every statement.
Best Value
Where will ASI not supply the semicolon you expect?
It will not create an empty statement
ASI does not insert a semicolon if doing so would create an empty statement. For example, putting else on a new line does not repair a missing consequent:
if (a > b)
else c = d;
This remains invalid; a newline does not give the if its required consequent.
It will not fill the required separators in a for header
A for header requires its two separator semicolons as part of its syntax. ASI does not insert either one to complete an incomplete header such as for (a; b, even if a line break appears before a closing parenthesis.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
How should you format code to avoid ASI surprises?
- Keep the expression on the same line after
returnorthrow; keep a label on the same line afterbreakorcontinue. - Keep postfix
++and--attached to their operand, and observe the restricted placements aroundyield, arrow syntax, andasync. - Before starting a new statement with
(,[, a template literal, unary+or-, or a slash-starting expression, explicitly end the prior statement or make the structure unambiguous. - Do not rely on a newline to fix an incomplete construct such as an
ifbranch or aforheader.
These habits prevent accidental continuations while preserving the distinction between a useful explicit semicolon and the claim that JavaScript requires one at every line ending.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




