JavaScript Statements: Expression Statements, Blocks, Empty Statements and Declarations
What statements are and how they differ from expressions, expression statements and the code that can't start one, blocks and block scope, the empty statement and the stray-semicolon bug, the declaration statements, and how the kinds of statements fit together. Starts Part 5.
kvi5hnuCode Sprout team member7 min readOn this page
If expressions are the phrases of JavaScript, statements are its sentences. An expression is evaluated to produce a value; a statement is executed to make something happen: declare a variable, store a value, repeat some work, choose a path, leave a function. A JavaScript program is a sequence of statements, run one after another from top to bottom unless a statement changes that order.
Part 5 covers every kind of statement. This article covers the basic building blocks: expression statements, blocks, the empty statement and declarations. The articles after it cover the statements that control the flow of a program: conditionals, loops, jumps and exceptions.

Expression statements
The simplest statement is an expression written on its own, followed by a semicolon. It's only useful if the expression has a side effect, since its value is thrown away:
total += price; // assignment
count++; // increment
delete session.token; // property deletion
saveDraft(form); // function call
await flushQueue(); // waiting for an async operation
A function call without side effects makes a useless statement: Math.max(a, b); computes a value and discards it. Store or use the result: const largest = Math.max(a, b);.
What can't start an expression statement
Three things are ambiguous at the start of a statement, so JavaScript always parses them as something other than an expression:
| Statement starts with | JavaScript reads it as |
|---|---|
{ | a block, not an object literal |
function | a function declaration, not a function expression |
class | a class declaration, not a class expression |
That's why some code needs extra parentheses:
const attempts = [
"{ a: 1, b: 2 }", // a block with a label: syntax error at the comma
"({ a: 1, b: 2 })", // an object literal
"function () { return 1; }()", // a function declaration needs a name
"(function () { return 1; })()", // an immediately invoked function expression
];
for (const code of attempts) {
try {
console.log(code.padEnd(34), JSON.stringify(eval(code)));
} catch (err) {
console.log(code.padEnd(34), err.name);
}
}
{ a: 1, b: 2 } SyntaxError
({ a: 1, b: 2 }) {"a":1,"b":2}
function () { return 1; }() SyntaxError
(function () { return 1; })() 1
You've seen this already with destructuring assignment, ({ a } = obj) (article 28), and with arrow functions returning objects, () => ({ ok: true }) (article 29). The second pair is the classic IIFE (immediately invoked function expression), which was the standard way to create a private scope before modules and block-scoped let existed. You'll still meet it in older code and bundler output.
Blocks: grouping statements
A block (or compound statement) is a group of statements inside curly braces. Wherever the syntax allows one statement, a block lets you put any number:
if (cart.isEmpty()) {
showMessage("Your cart is empty");
disableCheckout();
}
Two details:
- A block doesn't end with a semicolon. The statements inside it do.
- Since ES2015, a block creates a scope:
let,constandclassdeclared inside are invisible outside it.varignores blocks (article 26).
{
let insideOnly = 1;
var leaksOut = 2;
}
console.log(typeof insideOnly, leaksOut);
undefined 2
A bare block like this, not attached to if or a loop, is legal. It's occasionally useful to keep temporary variables from leaking into the rest of a long function, or to reuse a name in each case of a switch (article 40).
The empty statement
A semicolon on its own is the empty statement: it does nothing. It exists so you can write a loop or if with no body:
const squares = [];
for (let i = 0; i < 4; squares[i++] = i * i) /* empty */ ;
console.log(squares);
[ 1, 4, 9, 16 ]
All the work happens in the loop header. (Notice also the evaluation order from article 30: squares[i++] fixes the target index before i * i uses the incremented i.) Code like this is clever, not clear; a normal loop body is easier to read. If you do write an empty body, mark it with a comment.
The empty statement's real fame comes from bugs. A stray semicolon after an if or loop header becomes the body:
let alerts = 0;
const temperature = 20;
if (temperature > 40); // the empty statement is the whole if body
alerts++; // always runs: it's just the next statement
console.log(alerts);
1
The indentation lies; the engine doesn't care about it. Two habits prevent this class of bug entirely: always use braces for bodies (ESLint's curly rule), and let a formatter fix the indentation, which makes the mistake visible. ESLint's no-empty rule catches empty blocks left by mistake.
Declaration statements
Declarations are statements that create names:
| Declaration | Creates | Covered in |
|---|---|---|
let, const, var | variables and constants | articles 25–26 |
function name() {} | a function, hoisted with its body | article 26, part 8 |
async function, function* | async and generator functions | parts 12–13 |
class Name {} | a class (in the TDZ until declared) | part 9 |
import, export | module bindings | part 10 |
using, await using | resources disposed automatically (ES2027) | part 13 |
Declarations follow scope rules: let, const, class and (in strict code) function declarations belong to the nearest enclosing block, while var belongs to the nearest function. import and export may only appear at the top level of a module.
Function declarations inside blocks are a historical gray area. In strict code, which includes modules and classes, a function declared inside a block is scoped to that block:
(function () {
"use strict";
if (true) {
function helper() { return 1; }
console.log(typeof helper);
}
console.log(typeof helper);
})();
function
undefined
In sloppy (non-strict) code, browsers apply legacy rules that can also make the function visible outside the block. Don't depend on either behavior: declare the function at function or module level, or assign a function expression to a const inside the block.
Statements have completion values
A curiosity that explains some console behavior: every statement has a completion value, the value it "ends with". You never use it in normal code, but eval, the browser console and the Node REPL display it:
for (const code of ["1; 2; 3", "if (true) { 'yes' }", "let z = 5", "{ a: 1 }"]) {
console.log(code.padEnd(20), eval(code));
}
1; 2; 3 3
if (true) { 'yes' } yes
let z = 5 undefined
{ a: 1 } 1
This is why the REPL prints undefined after let z = 5 (article 2), and why typing { a: 1 } in a console may show 1: it's a block containing the label a and the expression 1. (Browser consoles and newer Node REPLs try to wrap input that starts with { in parentheses, so they may show an object instead.)
Common questions
Do I need a semicolon after a block, an if or a function declaration?
No. Statements that end with a block (if, for, while, function declarations, class declarations, plain blocks) don't take a semicolon. A semicolon after them is an empty statement: harmless after a function declaration, but a bug after an if (...) header. Statements like let x = 1, return value and expression statements end with a semicolon (or rely on ASI, article 8).
Is this code a declaration or an expression?
const fn = function () {};
Answer
Both are involved. The statement is a const declaration; its initializer is a function expression. Unlike a function declaration, the function isn't hoisted: it exists only after this line runs (article 26).
Why does JavaScript treat { as a block instead of an object at the start of a line?
Because a line can't be both. The grammar resolves the ambiguity in favor of blocks, which are far more common at statement level. Anywhere an expression is expected (after =, inside parentheses, as an argument), { starts an object literal.
Can a statement appear where an expression is expected?
No. You can't write const x = if (a) 1 else 2 or pass a for loop as an argument. Use expression forms: the conditional operator a ? 1 : 2, array methods such as map, or wrap the statements in a function and call it.
Summary
- Statements execute to make something happen; expressions evaluate to values. Programs are sequences of statements.
- An expression statement is an expression with a side effect followed by
;. Statements can't begin with{,functionorclassas expressions; wrap them in parentheses. - A block
{ ... }groups statements and creates a scope forlet,constandclass. - The empty statement
;does nothing; after anifor loop header, it silently becomes the body. Always use braces. - Declarations (
let,const,var,function,class,import,export,using) create names with specific scope and hoisting rules.
Practice Questions
What is logged by this code, and why?
let n = 0;
for (let i = 0; i < 3; i++);
n++;
console.log(n);
Answer
1. The semicolon after the loop header is an empty statement that serves as the loop body. The loop runs three times doing nothing, then n++ runs once.
Why does this code fail, and how do you make it run immediately?
function () {
console.log("hi");
}
();
Answer
At the start of a statement, function begins a declaration, which needs a name, so it's a SyntaxError. Wrap it in parentheses to make it an expression and call it: (function () { console.log("hi"); })();.
After this block, which of the two names can later code use?
{ const secret = 1; var visible = 2; }
Answer
Only visible. const is scoped to the block; var is scoped to the enclosing function or script and ignores blocks.
Is getTotal; a valid statement? Is it useful?
Answer
It's valid: an identifier is an expression, so this is an expression statement. It's useless, since it reads the value and discards it without calling the function (and throws a ReferenceError if getTotal doesn't exist). It's usually a bug where getTotal(); was intended. ESLint's no-unused-expressions rule catches it.
Previous: JavaScript Conditional (Ternary), typeof, delete, void and await Operators
Next: JavaScript if, else and else if: Conditionals, Braces, Guard Clauses and Common Mistakes

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.