Skip to content
blog
2 min read

Semicolons

Learn about semicolons in JavaScript, including how automatic insertion works and the impact on code readability.

Semicolons end statements in JavaScript. They’re technically optional because JavaScript has Automatic Semicolon Insertion (ASI) — but that mechanism has edge cases worth knowing about.

What Semicolons Do

const name = "Reza";
console.log(name);

These two statements are separated by semicolons. The parser knows where one ends and the next begins.

Automatic Semicolon Insertion

JavaScript inserts semicolons automatically in most places where they’re missing. This works reliably for the vast majority of code:

const name = "Reza"; // ASI adds ; here
console.log(name); // ASI adds ; here

The Dangerous Cases

ASI fails when a line starts with (, [, /, +, or -:

const a = 1;
const b = (2)[(a, b)].forEach(console.log);
// Interpreted as: const b = 2[a, b].forEach(...)
// TypeError

Fix:

const a = 1;
const b = 2;
[a, b].forEach(console.log);

Consistent Style Matters More Than the Choice

The two camps:

  • With semicolons — explicit, no ASI surprises
  • Without semicolons — cleaner visually, relies on ASI

Both are valid. What’s not valid is mixing randomly within a project. Pick one, use a linter (ESLint + Prettier), and stop thinking about it.

Most modern codebases use Prettier which enforces semicolons (or not) automatically. Let the tool decide.