Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

Semicolons in JavaScript: When They’re Required and How ASI Works

JavaScript does not insert a semicolon at every newline. Learn the ASI cases, restricted line breaks, expression-continuation traps, and practical formatting rules.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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…while statement.
  • 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How should you format code to avoid ASI surprises?

  • Keep the expression on the same line after return or throw; keep a label on the same line after break or continue.
  • Keep postfix ++ and -- attached to their operand, and observe the restricted placements around yield, arrow syntax, and async.
  • 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 if branch or a for header.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.