The core distinction

An expression is a piece of JavaScript that produces a value. The number 7 is an expression, as are 3 + 4 and name.toUpperCase(). JavaScript can use the resulting value in a larger expression: 3 + 4 can become part of (3 + 4) * 2.

A statement is an instruction that performs an action or controls how code runs. A variable declaration such as const total = 3 + 4; is a statement. Inside it, 3 + 4 is an expression whose value is assigned to total. This distinction is useful because expressions can go where JavaScript expects a value, but statements generally cannot.

To follow the examples, you need a modern browser’s developer console or a JavaScript runtime that supports ES2015 features such as const and let. No library or project setup is required. You can type the examples into the console one at a time, though a console may display a result for a statement too. That display alone does not mean the entire statement is an expression you can place inside another expression.

Expressions can be small or complex

Literals such as "hello", true, and 12 are expressions. A variable reference is also an expression: after const price = 12;, the name price can be used wherever its value is needed. Operators combine expressions. For example, price * 2 produces a number, while price > 10 produces a Boolean value.

Function calls are expressions too. In "hello".toUpperCase(), the result is "HELLO". A call still counts as an expression when its return value is undefined. For instance, console.log("Ready") prints text but returns undefined. Producing a value does not necessarily mean producing a value you want to keep.

An expression can also change program state. If count has been declared with let, the assignment count = count + 1 produces the new assigned value as well as updating count. Do not use “changes something” as the test for a statement. Ask instead whether the code can be used where a value is expected.

Statements organize a program

Common statements include declarations, if statements, loops, and return statements. Consider: const score = 8; if (score >= 5) { console.log("Pass"); }. The declaration stores a value. The if statement decides whether to run its block. The comparison score >= 5 is an expression inside that statement, and the call to console.log is another expression used as an expression statement.

An expression statement is an expression written as a standalone instruction. For example, console.log("Pass"); calls a function without using its return value. The semicolon ends that statement; it does not turn every statement into an expression. You cannot write const result = if (score >= 5) { "Pass" }; because an if statement cannot occupy the place where the value for result is required.

A block, marked by braces, groups statements. In if (score >= 5) { console.log("Pass"); }, the braces contain what runs when the condition is true. Braces have other uses in JavaScript, including object literals, so their meaning depends on the surrounding syntax.

Choosing between if and a conditional expression

When you need a value based on a condition, JavaScript provides the conditional expression, often called the ternary operator. For example: const label = score >= 5 ? "Pass" : "Try again";. JavaScript evaluates the condition, then produces the value from one of the two alternatives. Because the whole conditional is an expression, it can supply the value for label.

An if statement is often clearer when each branch needs several instructions. For example, you might use if and else to print different messages and update separate variables. Use a conditional expression for a short choice between values, not as a rule that every if must be replaced. In either form, the condition itself is an expression.

A practical way to check your understanding

Look at a line in parts rather than trying to classify the whole line by its punctuation. In const doubled = price * 2;, the full line is a declaration statement; price * 2 is the expression providing its value. In console.log(doubled);, the function call is an expression used as a statement. In if (doubled > 20) { console.log("High"); }, the comparison supplies a Boolean value to a control-flow statement.

When you are unsure, try asking: “Could I use this as the value after an equals sign in a declaration?” You could write const answer = 3 + 4; or const answer = score >= 5 ? "Pass" : "Try again";. You could not put a complete if statement there. This check is a helpful starting point, though JavaScript syntax has context-specific rules. The main idea remains simple: expressions supply values, and statements put those values to work or control what happens next.

Back to category ↗