Programming Search⌘K

Featureintro

Expressions and operators

Also called expression, operator, operand, binary operator, unary operator, ternary operator, evaluation order.

An expression is code that resolves to a value. Operators join operands into larger expressions. Precedence and associativity set the grouping, and the operands are evaluated from left to right.

Language
JavaScript

Synopsis

ECMA-262 section Operators Page
13.2 Primary Expression this, names, literals, ( )
13.3 Left-Hand-Side Expressions a.b, a[b], a?.b, f(), new C()
13.4 Update Expressions ++, -- Increment and decrement
13.5 Unary Operators delete, void, typeof, +, -, ~, !, await
13.6 to 13.8 **, *, /, %, +, - Arithmetic operators
13.9 Bitwise Shift Operators <<, >>, >>> Bitwise operators
13.10 Relational Operators <, >, <=, >=, instanceof, in
13.11 Equality Operators ==, !=, ===, !== Comparison operators
13.12 Binary Bitwise Operators &, ^, | Bitwise operators
13.13 Binary Logical Operators &&, ||, ??
13.14 Conditional Operator c ? a : b
13.15 Assignment Operators =, +=, &&= and the others Assignment operators
13.16 Comma Operator a, b

The sections are those of clause 13.1 The order of the rows follows the grammar, from the primary expressions down to the comma.1

Semantics

r1. Expressions. An expression "is a valid unit of code that resolves to a value."2 MDN names two types: "those that have side effects (such as assigning values) and those that purely evaluate."2 x = 7 is of the first type, and its value is 7. 3 + 4 is of the second type.2 Example: kinds.

r2. Operators. "Where an expression contains slots for further nested expressions, the part(s) that are not slots are known as operators."3 In a + b, the operator is +, and "each operator corresponds to an expression."3

r3. Operand count. A binary operator takes two operands, and "All binary operators in JavaScript are infix."2 A unary operator takes one operand. MDN says that ++ and -- are the only postfix operators, and that the other unary operators are prefix.2 The conditional operator c ? a : b is the one ternary operator.2 Example: kinds.

r4. Grouping. "The precedence of operators determines the order they are applied when evaluating an expression."2 So 1 + 2 * 3 is 7. Parentheses change the grouping.2 Within one level of precedence, associativity sets the grouping: "Assignment operators are right-associative".4 The operator precedence page of MDN has the full table.4

r5. Evaluation order. Precedence and associativity "only affect the order of evaluation of operators (the implicit grouping), but not the order of evaluation of operands."4 "The operands are always evaluated from left-to-right."4 The logical operators &&, || and ?? and the conditional operator can skip an operand.4 Example: order.

r6. Assignment targets. The left side of = must be a valid target: "It is a Syntax Error if the AssignmentTargetType of LeftHandSideExpression is not simple."1 This is a compile error, so no part of the code runs. Example: bad-target.

r7. Discarded values. A value that is not part of a bigger construct is discarded. MDN calls this usually a programmer mistake, if the expression has no side effects.2 Statements and declarations covers the expression statement.

Examples

The examples run with Node.js 24.

An assignment with a side effect and an addition without one, then precedence, unary and postfix operators, the ternary operator and the comma operator (rules r1, r3 and r4):

let x;
console.log(x = 7, 3 + 4, x);
console.log(1 + 2 * 3, (1 + 2) * 3);
console.log(typeof x, -x, !x, x++, x, ++x);
console.log(x > 5 ? "big" : "small");
console.log((x = 1, x + 1));
Output
7 7 7
7 9
number -7 false 7 8 9
big
2

The operands run from left to right, also where precedence groups the right side first (rule r5). * binds tighter than +, and ** groups from the right, but the calls still run in the order of the source:

const echo = (name, value) => {
  console.log("evaluating", name);
  return value;
};
console.log(echo("a", 1) + echo("b", 2) * echo("c", 3));
console.log(echo("d", 2) ** echo("e", 3) ** echo("f", 2));
Output
evaluating a
evaluating b
evaluating c
7
evaluating d
evaluating e
evaluating f
512

n + 1 is not a place to store a value (rule r6):

This code raises SyntaxError, on purpose.

let n = 1;
n + 1 = 3;

Node.js reports SyntaxError: Invalid left-hand side in assignment.

Pitfalls

Precedence does not set the order of side effects

In f() + g() * h(), f runs first, although the multiplication groups first (rule r5). Code that needs a fixed order of side effects must use separate statements.

A discarded expression

x + 1; alone is a valid statement, but it changes nothing. x === 1; in place of x = 1; gives no error. A linter can find such statements.

Assignment inside a condition

= is an operator, so if (x = 0) is valid. It stores 0 and tests the value 0, which is false. Use === for a test (Comparison operators).

Comparison

Topic JavaScript Python
assignment = is an operator inside expressions1 = is a statement; := is an assignment expression5
evaluation order operands from left to right4 "Python evaluates expressions from left to right."5
the right side of = the target reference is evaluated first1 "the right-hand side is evaluated before the left-hand side"5
ternary c ? a : b2 a if c else b5

Python calls := an "assignment expression (sometimes also called a “named expression” or “walrus”)".5 See Assignment expressions and Operator precedence in Python.

References

  1. ECMAScript 2026 Language Specification, 13 ECMAScript Language: Expressions; 13.15.1 Static Semantics: Early Errors
  2. Expressions and operators, MDN JavaScript Guide, introduction
  3. Expressions and operators, MDN, Expressions and operators by category; What are operators?
  4. Operator precedence, MDN, Precedence and associativity
  5. Expressions, The Python Language Reference, 6.12 Assignment expressions; 6.13 Conditional expressions; 6.16 Evaluation order

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source