Here's a function that looks like it returns an object:
function config() {
return
{
retries: 3
}
}
config(); // undefined
It returns undefined. No error, no warning, just a quietly wrong value. The culprit is automatic semicolon insertion
(ASI), a set of rules in the ECMAScript specification that adds semicolons where your code doesn't have them.
What ASI actually does
The spec inserts a semicolon in three situations, roughly:
- The next token can't legally continue the statement, and there's a line break before it.
- The next token is a closing brace
}. - The end of the input is reached.
On top of that, some grammar rules forbid a line break in a specific spot. These are called restricted productions. If you put a line break there, a semicolon is inserted. The ones that bite are:
return,breakandcontinuefollowed by a line break.++and--used after a value (postfix) on the next line.
So return followed by a new line becomes return;, and the object on the next line becomes an unreachable block.
The fix: put the start of the value on the same line as return.
return {
retries: 3,
};
The opposite trap: lines that join
ASI only inserts a semicolon when the next line can't continue the statement. If it can, the lines join. That
catches code written without semicolons when a line starts with (, [ or a template literal:
const total = price * quantity
[1, 2, 3].forEach(log)
That reads as price * quantity[1, 2, 3].forEach(log), which is nonsense at runtime. Likewise, a line starting with
( turns the previous line into a function call.
Developers who skip semicolons put one at the start of these lines:
;[1, 2, 3].forEach(log)
Semicolons or not?
Both styles are fine if a tool enforces them. Arguing about it in code review is the expensive option. Pick one, let a formatter like Prettier apply it on save, add a linter rule for unreachable code, and the tricky cases above get caught automatically.
The real lesson is bigger than semicolons: small details cause big bugs when they rely on people remembering rules. Move the rule into a tool, and nobody has to remember.










