Skip to content

Expression

In one line

An expression is any piece of code that evaluates to a single value, like 2 + 3 or "Hi, " + name.

In simple words

An expression is a fragment of code that produces a value. 2 + 3 is an expression (it evaluates to 5). "Hi, " + name is an expression (it evaluates to a string). Even a bare variable like age is an expression — it evaluates to the value the variable holds.

Expressions can be combined and nested. add(2, 3) * 4 is an expression made of a function call expression, a literal expression, and a multiplication. Anywhere JavaScript expects a value, you can put an expression: in variable assignments, function arguments, array elements, and more.

The counterpart to an expression is a statement — a complete unit of code that performs an action, like if, for, or return. Statements do not produce values. Most programs are a mix of statements that contain expressions.

Example

javascript
// Each of these is an expression that produces a value:
5;
"hello";
2 + 3;
Math.max(1, 2);
[1, 2, 3].length;

// Expressions can be used anywhere a value is expected:
const total = 10 + 5;       // 10 + 5 is an expression
console.log(2 * 3 + 1);     // 2 * 3 + 1 is an expression

Every line above is an expression — it evaluates to a value. Expressions can be nested inside other expressions.

Common confusions

  • Confused with: statement

    The difference: An expression evaluates to a value (2 + 3 → 5); a statement performs an action and does not produce a value (if, for, return). Some constructs like x = 5 are both — they perform assignment and evaluate to the assigned value.

Related terms

Related Resources

Related Lessons

  • Variables

    Expressions are assigned to variables

Learn the fundamentals

Deepen your understanding with structured lessons on this topic.

View lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary