Sub topic 1
Operators, Operands, and Expressions
Every comparison, default value, and calculation in JavaScript is built from the same few parts. This chapter names those parts and shows the order in which JavaScript evaluates them. The rest of the tutorial builds on that.
Prerequisites
This chapter assumes you can already:
- Declare variables with
letandconst. - Recognize JavaScript's primitive types (number, string, boolean, null, undefined, bigint, symbol) and objects.
- Write and call functions.
- Run code in a browser console or Node.js.
The Three Parts
An operator is a symbol or keyword that produces a value from one or more inputs, such as +, ===, typeof, or ?.. An operand is one of those inputs. An expression is any piece of code that evaluates to a value. A literal like 42 is an expression, a variable name is an expression, and an operator applied to operands is an expression.
Because the operands can themselves be expressions, small expressions combine into larger ones.
Counting Operands: Arity
Operators are grouped by how many operands they take:
| Kind | Operands | Examples |
|---|---|---|
| Unary | 1 | `-x`, `!flag`, `typeof v`, `++i` |
| Binary | 2 | `a + b`, `a === b`, `a && b` |
| Ternary | 3 | `cond ? a : b` (the only one) |
let x = 5;
const y = -x; // unary: one operand
const sum = x + y; // binary: two operands
const label = sum === 0 ? 'zero' : 'nonzero'; // ternary: three operands
console.log(y, sum, label); // -5 0 zeroExpressions Versus Statements
A statement performs an action, for example if, for, or a let declaration. An expression produces a value. The two overlap because assignment is an expression: it evaluates to the value that was assigned.
let a;
console.log(a = 3); // 3: the assignment expression has a value
let b, c;
b = c = 10; // c = 10 evaluates to 10, which is then assigned to b
console.log(b, c); // 10 10This is why if (a = 0) is a common bug. It assigns 0 and then tests that value, when the author meant to compare.
Evaluation Order of Operands
JavaScript evaluates operands left to right, then applies the operators. Precedence controls how the expression is grouped, but it does not change the order in which operands run.
function log(v) {
console.log('evaluated', v);
return v;
}
const r = log(1) + log(2) * log(3);
console.log(r);
// evaluated 1
// evaluated 2
// evaluated 3
// 7The multiplication is applied first, yet log(1) still runs first. This matters when operands have side effects, such as function calls, ++, or assignments.
The tree shows the grouping. Operands are still visited left to right: 1, then 2, then 3. The next chapter explains how precedence produces this tree.
Exercise
Given let i = 1; const r = i + (i = 5) + i;, what is r, and why?
Show answer
r is 11. The operands are evaluated left to right: the first i is 1, then (i = 5) assigns 5 and evaluates to 5, then the last i is now 5. So 1 + 5 + 5 = 11.
Exercise
Is x > 0 ? 'pos' : 'non-pos' an expression or a statement, and how many operands does its operator take?
Show answer
It is an expression, because it evaluates to a value. The conditional operator is ternary, so it takes three operands: the condition, the value used when the condition is true, and the value used when it is false.