By the end of this lesson, you will be able to write JavaScript statements, terminate them with semicolons, and understand when automatic semicolon insertion can safely fill in missing semicolons.
What it is
A JavaScript statement is a complete instruction that tells the engine to do something. An expression produces a value, while a statement performs an action. Variable declarations, assignments, if blocks, loops, and return are statements. A semicolon (;) marks the end of many statements. JavaScript also uses automatic semicolon insertion (ASI), which adds missing semicolons when the parser reaches a line break and the next token cannot continue the current statement, or when a restricted statement such as return is followed by a line break.
Related terms: expression, block, declaration, ASI, empty statement.
Why it matters
- Statements are the basic units of JavaScript programs.
- Clear termination helps avoid subtle bugs caused by ASI.
- Statements let you store data, make decisions, repeat work, and return results.
- Consistent semicolon style makes code easier for teams and tools to read.
Syntax or steps
The smallest useful pattern is one instruction per statement, ending with a semicolon:
let a = 1;
a = a + 1;
- Declare a variable with
let,const, orvar. - Use an expression to compute a value.
- End the statement with
;. - Use braces
{ }when a statement needs a block of multiple statements.
Example
let a = 1;
a = a + 1;
if (a > 1) {
console.log("a is greater than 1");
}
let b = a * 2;
console.log(b);
Part by part:
let a = 1;declaresaand assigns1.a = a + 1;readsa, adds1, and stores the result back ina.if (a > 1) { ... }is a control statement with a block.let b = a * 2;creates a new variable from the updated value.console.log(b);prints4.
Common mistakes
- Putting a semicolon after an
ifcondition.if (a > 1); { ... }makes theifcontrol an empty statement. Remove the semicolon. - Breaking a
returnstatement.returnfollowed by a line break can cause ASI to insert a semicolon, returningundefined. Keep the returned value on the same line asreturn. - Assuming every line break ends a statement. If one line ends with
x = yand the next line starts with(z), JavaScript may parse it as a function call. Add a semicolon afteryor use parentheses carefully. - Using ASI inconsistently. Some missing semicolons work, but relying on them can make code harder to predict. Use semicolons explicitly in team code.
When to use it
Compare explicit semicolons with relying on ASI:
| Approach | Use when | Benefit | Risk |
|---|---|---|---|
| Explicit semicolons | Most production code, especially team projects | Clear intent and fewer surprises | Can feel noisy if overused |
| ASI | Small scripts or style guides that omit semicolons | Shorter code | Subtle parsing bugs in edge cases |
Practice
Guided exercise: write a statement that declares count as 5, increments it by 1, and logs the result.
let count = 5;
count = count + 1;
console.log(count);
Expected output: 6.
Challenge: rewrite this code so it does not accidentally return undefined.
function makeUser() {
return
{
name: "Ada"
};
}
Solution hint: move the object literal to the same line as return.
Quick check
Question: What does ASI do?
Answer: It inserts missing semicolons when the parser reaches a line break and the next token cannot continue the current statement, or when a restricted statement such as return is followed by a line break. It does not make every line break a safe statement boundary.
Summary
JavaScript statements are complete instructions, and semicolons make their boundaries explicit. ASI can help, but relying on it can create subtle bugs, especially with return, if, and line breaks.