Should You Use Semicolons in JavaScript? Pros, Cons, and Best Practices
This article examines the long‑standing debate over mandatory semicolons in JavaScript, explains Automatic Semicolon Insertion, shows code examples where omitting them causes errors, compares major style guides, and shares expert opinions to help developers choose a consistent approach.
Just as there is a sweet‑vs‑savory debate for zongzi, frontend developers argue over using semicolons in JavaScript. Some insist on always adding them, while others consider them unnecessary in many cases.
Origin of the Issue
JavaScript does not require a semicolon after every statement because the engine can often infer statement boundaries and automatically insert them (Automatic Semicolon Insertion, ASI). However, ASI rules can cause errors in certain patterns.
Many libraries start with a leading semicolon, for example:
;(function(){})()If the preceding semicolon is missing, concatenating this code with other code can produce a syntax error:
var name = 3
(function(){})()
// Without the semicolon, the parser treats it as:
var name = 3(function(){})()When a statement begins with
+,
-,
[,
(, or
/, you must be especially careful to add a semicolon, often at the start of the line.
The ECMAScript specification does not mandate semicolons; developers follow personal or team habits, and JavaScript cannot be written completely without them.
Differences
The book JavaScript: The Definitive Guide (4th edition) notes:
Even if a trailing semicolon is not strictly required, you should add it. Adding semicolons helps prevent problems caused by omission, avoids syntax errors when compressing code, and can improve parser performance.
Because most front‑end code is minified with tools like Webpack, whether the output retains semicolons is usually configurable.
Code Style Guidelines
Different style guides have opposite rules:
Google JavaScript Style Guide – requires semicolons.
Airbnb JavaScript Style Guide – enforces semicolons:
semi: ['error', 'always']JavaScript Standard Style – forbids semicolons:
"semi": ['error', 'never']Both Airbnb and Standard rely on the ESLint
semirule, yet they adopt opposite defaults, and both are widely used.
Expert Opinions
On Zhihu, several prominent developers shared their views:
Yu Bo: Follow the project’s convention. If the project omits semicolons (e.g., Zepto), keep it that way; if it uses them (e.g., jQuery), keep them. Both 2‑space and 4‑space indentations are fine.
Yu Yuxi: Strongly prefers no semicolons. Vue.js code has none, and Prettier now supports a no‑semicolon option, making migration between styles almost cost‑free.
Final Summary
Personal habits differ, but a project should maintain a consistent approach. Code formatters and editor plugins can automatically handle semicolons, ensuring uniform code style across the team.
KooFE Frontend Team
Follow the latest frontend updates
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.