Chapter I · The Language

Expressions

An expression is any valid unit of code that resolves to a value.

01 What is an Expression?

An expression is a combination of literals, variables, operators, and function calls that evaluates down to a single value.

Wherever JavaScript expects a value, you can supply an expression. They can be nested, combined, and evaluated inside larger statements throughout your code.

02 Simple Example

Math and string combination expressions evaluating into concrete values.

expression.js
let sum = 10 + 5;
let greeting = "Hello " + "World";
console.log(sum); // 15

10 + 5 and "Hello " + "World" are expressions that compute new values.

03 How It Works

The engine computes expressions by resolving variables and applying operator precedence rules until a final value remains.

score identifier 10 value in memory binds to let score = 10; — the name and the value are linked, not fused.

Expressions resolve down to single values that bind to identifiers or pass into statements.

04 Step-by-Step

  1. The engine identifies operand values and operators within the expression.

  2. Precedence rules determine evaluation order.

  3. The expression reduces to a single evaluated value.

05 Mental Model

"An expression is a math problem or calculation that yields an answer."

It takes inputs, computes them, and leaves behind a final resulting value ready for use.

06 Common Mistakes

Mixing types in "+" without meaning to. "5" + 3 produces the string "53", not the number 8, because + favors string concatenation when either operand is a string. This is a frequent source of unexpected UI bugs when a value from an input field (always a string) gets added to a number.

Best practice: convert explicitly with Number(value) before doing arithmetic on data that might be a string, so intent is clear at a glance.

07 Interview Questions

Q: Give an example of an expression that is also a valid statement.

A function call like doSomething(); — it's an expression (produces a return value) used here as a statement (its value is discarded).

Q: Is an assignment like x = 5 an expression or a statement?

It's an expression — it evaluates to the assigned value, which is why you can chain assignments like a = b = 5.

Key Takeaway

Expressions are code fragments that resolve to values, forming the building blocks of computations within statements.

Related Concepts