October 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 NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Semicolons vs. No Semicolons in JavaScript: Trade-Offs and Pitfalls

JavaScript semicolons are a style and tooling choice, but automatic semicolon insertion is not as simple as “newline means statement end.” Learn the key pitfalls and how to choose a consistent convention.

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.

Do you need semicolons in JavaScript? Not after every statement: ECMAScript allows many statement-ending semicolons to be omitted, using automatic semicolon insertion (ASI). But ASI is a set of grammar rules, not a rule that every newline ends a statement. Use the formatter and lint configuration already established by your project, and learn the line breaks that can change how code is parsed.

What automatic semicolon insertion does—and does not do

Ecma International’s ECMAScript 2026 specification, §12.10, says: “Most ECMAScript statements and declarations must be terminated with a semicolon.” It also permits semicolons to be omitted in specified situations. The parser inserts them when grammar and token boundaries meet the specification’s rules—for example, when a token cannot fit the grammar and a line terminator, closing brace, or end of input provides the relevant boundary, or when a restricted production forbids a line break.

As an Amazon Associate I earn from qualifying purchases.

There are exceptions: insertion does not happen if it would create an empty statement or become one of the two separators in a for header. So a newline is not, by itself, a reliable statement terminator. The same source can be parsed differently than its visual layout suggests.

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

Where omitting semicolons can change the result

A newline after return ends the return

function getValue() {
  return
  { answer: 42 }
}

This function returns undefined. The line terminator after return causes a semicolon to be inserted, so the object literal is not the return value. Put the expression on the same line: return { answer: 42 }. Similar restricted line-break rules apply to throw and yield; keep the intended expression on the same line as the keyword.

A leading parenthesis or bracket can continue the previous expression

const settings = {}
(function () {
  configure(settings)
})()

The next line begins with (, so it can be parsed as continuing the preceding expression rather than starting an independent statement. In this case, the parser can treat the code like a call on the object. A line beginning with [ can create a similar continuation hazard.

With explicit semicolons, terminate the assignment: const settings = {};. In a semicolon-free style, a defensive leading semicolon separates the expressions:

const settings = {}
;(function () {
  configure(settings)
})()

ESLint documents this kind of ambiguity in its semi rule guidance, and Standard Style documents the leading-semicolon convention in its semicolon rules.

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

Other line breaks have special rules

Keep postfix ++ or -- with its operand; put an expression on the same line as return, throw, or yield; keep a label with break or continue; and keep arrow parameters with => and async with the function or method token that follows. Depending on the construct, a line break in these positions can force insertion or cause a syntax error. The specification’s ASI rules define the behavior.

How the two styles compare

Consideration Explicit semicolons Omitting statement-ending semicolons
Statement boundaries Marks endings visibly, which can make boundaries easier to scan. Relies more on readers and tools to recognize where grammar ends one statement and begins another.
Punctuation Adds a consistent marker after statements that require termination. Reduces punctuation, but may need a leading defensive semicolon before certain next-line expressions.
ASI hazards Helps separate adjacent statements, but does not make a newline after return safe. Needs care around continuation characters such as ( and [, and the same restricted line-break rules still apply.
Project consistency Works when the formatter and lint rules agree on this convention. Also works when the formatter and lint rules agree; Standard Style documents this policy.

Neither convention is inherently faster or more correct at runtime. The meaningful difference is readability and tooling consistency; correctness depends on whether the code parses as intended. The cited guidance establishes available style policies, not a universally best choice.

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

Choose a convention your tools can enforce

Prettier

Prettier documents semi: true as its default. Setting semi: false omits statement-ending semicolons while retaining leading semicolons on lines where they protect against ASI problems. See Prettier’s semicolon option and its rationale for semicolons.

ESLint

The core ESLint semi rule documents always and never conventions, but the rule page says it was deprecated in ESLint v8.53.0 and directs users to the corresponding rule in @stylistic/eslint-plugin. Check the installed ESLint version, plugin, and project configuration before changing rule syntax. ESLint’s recommended configuration also enables no-unexpected-multiline, which disallows confusing multiline expressions; see the semi documentation.

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

Standard Style

JavaScript Standard Style’s semicolon policy omits statement-ending semicolons and uses a leading semicolon where needed to prevent an expression from continuing the previous one.

A practical way to decide

  1. Check the repository first. Look at existing files and the formatter and lint configuration; matching the project avoids noisy formatting changes and conflicting rules.
  2. Let one formatter own the output. Prettier supports either choice through its semi option. Avoid configuring a linter to demand output that the formatter immediately rewrites.
  3. Keep line-sensitive constructs together. In particular, do not put a line break after return when the next expression is its intended value, and review lines starting with ( or [.
  4. Review intentional exceptions. If a defensive leading semicolon appears in an otherwise semicolon-free file, it separates expressions; do not remove it without checking how the preceding line parses with the next one.

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
PC Slower Than It Used to Be?Free scan - under a minute
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.