Statements and Expressions

An expression produces a value; a statement is a complete instruction that may contain one but isn't one itself.

Purpose

Every line of JavaScript is either an expression — something that evaluates to a value — or a statement — a complete instruction, such as a declaration or a control-flow construct. Knowing which you’re looking at explains why some code can be used inside other code (like passing price * quantity as a function argument) and some can’t (you can’t pass an if block as an argument).

Expressions

An expression always produces a value:

5 + 3; // 8
'hi'.toUpperCase(); // "HI"
isReady ? 'go' : 'wait'; // one of the two strings

Expressions can nest inside other expressions and inside most statements.

Statements

A statement is a complete instruction. Declarations (let, const, function), control flow (if, for, while, switch), and return/break/continue are all statements:

let total = 0;

if (total === 0) {
  console.log('empty cart');
}

An expression statement is an expression used on its own as a statement, most often a function call for its side effect (console.log('hi');) — the value it produces, if any, is simply discarded.

Behavior notes

  • A statement can contain expressions (let x = 5 + 3; contains the expression 5 + 3), but a statement itself has no value you can assign or pass around.
  • Because if, for, and switch are statements, not expressions, JavaScript has no “if expression” — the closest equivalent for a single value is the conditional (ternary) operator, condition ? a : b.
  • Statements are terminated by a semicolon. When one is omitted, Automatic Semicolon Insertion (ASI) inserts one according to specific grammar rules — see Strict mode and the orientation lesson for a common ASI pitfall involving return.

Common mistakes

  • Assuming a block like if (x) { ... } produces a value the way an expression does — it doesn’t; nothing can be assigned from it directly.
  • Writing a function expecting an object literal to be returned, but leaving a line break between return and the opening { — ASI turns that into return; followed by unreachable code.

Runtime notes

Statements and expressions are defined by ECMAScript itself, so this behavior is identical in every JavaScript engine, browser or Node.

Taught in these lessons

Related reference