Automatic semicolon insertion — where a missing semicolon changes the meaning¶
Level: 201 · for anyone who leaves semicolons out, or works with someone who does
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: A line break ends a statement only when the next line cannot continue it, but always ends a return: return followed by a newline returns undefined, a line starting with ( calls the value above it, and one starting with [ indexes into it.
Keywords: automatic semicolon insertion, ASI
What the finished page will answer¶
- What does a function return when
returnis alone on its line and the value is on the next one? - What happens when a line without a semicolon is followed by one that starts with
(,[or a backtick? - Why does the swap
[a, b] = [b, a]throw aReferenceErrorright afterlet a = 1, b = 2written without a semicolon? - Where does a semicolon go in
i,++,jwritten on three lines, and what areiandjafterwards?
Examples it will need¶
-
semicolons_and_asi_three_traps_js.js— a return with its value on the next line, a line starting with ( and one starting with [, each with what it actually evaluated to -
semicolons_and_asi_swap_sh.sh— the destructuring swap after a line with no semicolon, with the ReferenceError and the exit status
See also¶
- Destructuring — the swap that a missing semicolon turns into a TDZ error
- Template literals — a line starting with a backtick becomes a tagged template
- Prettier — the semicolon setting in Prettier, and what it guards against
- ESLint — a lint rule for the unexpected multiline
- Rust: A block is an expression ↗ — in Rust, one semicolon changes what a block returns