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.
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:
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.
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:
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:
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:
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.
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:
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.
| Situation | Practical Choice |
|---|---|
| Learning JavaScript | Use semicolons |
| Existing project | Follow the project style |
| Formatter configured | Let the formatter decide |
| No-semicolon style | Learn 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
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.
Frequently Asked Questions
Are semicolons required in JavaScript?
Should beginners use semicolons?
Do semicolons affect performance?
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.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.