JavaScript Semicolons: Are They Required? A Complete Guide

Are semicolons required in JavaScript? Learn when they are optional, when they still matter, and how Automatic Semicolon Insertion affects your code.

5 min read

JavaScript semicolons are usually not required in the code you write because JavaScript has Automatic Semicolon Insertion, or ASI. ASI can insert missing semicolons while parsing.

That does not mean semicolons never matter. Some line breaks still change how code is read.

The Short Answer

Both styles can run. First, here is the explicit-semicolon version:

javascriptjavascript
const name = "Alice";
console.log(name);

The same code also works without written semicolons because ASI can separate these two statements safely. There is no confusing continuation between the variable declaration and the log call, so the parser can treat them as separate statements.

javascriptjavascript
const name = "Alice"
console.log(name)

Both versions print Alice. The difference is style and risk, not output.

Why Missing Semicolons Usually Work

JavaScript statements often require separators. If you omit a semicolon where the parser can safely insert one, ASI does it for you.

For example, these two variable declarations are separate statements:

javascriptjavascript
const x = 5
const y = 10
console.log(x + y)

The result is 15. ASI can treat each line as a separate statement.

Where Missing Semicolons Can Break Code

The risky cases happen when the next line can be read as a continuation of the previous line. Common beginner examples are lines starting with a bracket, parenthesis, slash, plus, minus, or template literal.

Here is a bracket example:

javascriptjavascript
const items = [1, 2, 3]
[1, 2].forEach(console.log)

JavaScript can read the second line as property access on the first array. That is not what the author meant.

A common no-semicolon style fix is to add a leading semicolon:

javascriptjavascript
const items = [1, 2, 3]
;[1, 2].forEach(console.log)

The leading semicolon makes the second line a separate statement.

The return Line-Break Problem

Semicolons also matter around statements where JavaScript does not allow a line break before the value.

javascriptjavascript
function getPrice() {
  return
    99
}
 
console.log(getPrice());

This prints undefined. ASI inserts a semicolon after return, so the function returns before reaching 99.

Put the returned value on the same line:

javascriptjavascript
function getPrice() {
  return 99;
}

The same caution applies to throw, break, continue, and yield.

Should You Use Semicolons?

For beginners, the safest answer is yes. Written semicolons remove a category of ASI surprises and make statement endings obvious.

In real teams, follow the project style. Many teams let a formatter make the decision automatically.

SituationPractical Choice
Learning JavaScriptUse semicolons
Existing projectFollow the project style
Formatter configuredLet the formatter decide
No-semicolon styleLearn ASI edge cases

The worst choice is mixing styles randomly. Consistency is what keeps code easy to scan.

Final Rule of Thumb

Use semicolons until you understand ASI well. If you later choose a no-semicolon style, protect lines that can continue the previous expression.

For broader formatting choices, see the JavaScript code style guide. You can also practice these examples in Chrome DevTools.

Rune AI

Rune AI

Key Insights

  • JavaScript semicolons are often optional, but statements still need separators.
  • ASI inserts semicolons only in specific parsing situations.
  • New lines that continue the previous expression can surprise you.
  • A line after return, throw, break, continue, or yield can change meaning.
  • Consistency and a formatter matter more than arguing over style.
RunePowered by Rune AI

Frequently Asked Questions

Are semicolons required in JavaScript?

Written semicolons are often optional because JavaScript can insert them automatically in specific places.

Should beginners use semicolons?

Yes, using semicolons is a safe default while learning because it avoids ASI edge cases.

Do semicolons affect performance?

No. They are syntax, not a runtime performance feature.

Conclusion

JavaScript often lets you omit semicolons, but the safest beginner habit is to use them consistently or let a formatter choose for you. If you omit them, learn the ASI edge cases and protect lines that can continue the previous expression.