Learn JavaScript
❯ getting_started.syntax

Syntax, Semicolons & ASI in JavaScript

$concept

JavaScript statements usually end with semicolons and blocks wrap in curly braces — familiar C-style surface. The twist is ASI (Automatic Semicolon Insertion): the parser inserts missing semicolons at line breaks in most positions, so code without semicolons usually works — until it doesn't (a line starting with [ or ( can merge with the previous one).

Community practice split for years; the safe modern rule is simple: always write semicolons (or always omit them with a linter enforcing safe style — never mix casually). Comments are // for lines and /* ... */ for blocks, identical to C.

$how it works
1

Statements and blocks

Statements end with semicolons; function bodies, loop bodies, and if-branches wrap in { } — whitespace itself means nothing.

2

ASI fills gaps

The parser auto-inserts semicolons at most line breaks — convenient until a [ or ( opens the next line and fuses two statements.

3

Pick a semicolon religion

Always-semicolons (safest default) or never-with-linter (StandardJS style) — both fine; accidental mixing is where bugs live.

4

Comments

// to end of line, /* ... */ across lines — same as C, used for notes and temporary disabling.

$example
let total = 0
total += 5;
console.log(total); // 5
/* multi-line
comment */
Output
5
1The first line omits its semicolon — ASI inserts it safely since the next line starts with a name.
2The // comment explains the expected value; the block comment spans lines without effect.
35 prints — mixed styles worked here, but the [(/ hazard is why consistency matters.
$use cases
Choosing always-semicolons as the default team style
Recognizing ASI hazards when lines begin with [ or (
Reading StandardJS-style codebases that omit semicolons deliberately
Commenting out blocks quickly with /* ... */ while debugging

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.