Operators#
JavaScript’s operator set covers arithmetic, comparison, logical,
bitwise, string, optional chaining, nullish, spread, and assignment
combinations. Coercion is the recurring trap; the operator
prefers strict equality (===) and explicit Number() /
String() casts.
For the values these act on, see Types.
Arithmetic#
Operator |
Meaning |
|---|---|
|
addition; with a string operand, concatenation |
|
subtraction, multiplication, division, modulo, exponentiation |
|
increment / decrement (prefix or postfix) |
1 + 2; // 3
"1" + 2; // "12" (string concat)
"5" - 2; // 3 (numeric coerce)
7 / 2; // 3.5
2 ** 10; // 1024
-7 % 3; // -1 (sign follows dividend, unlike Python / Lua)
Equality#
Operator |
Meaning |
|---|---|
|
loose; triggers implicit coercion ( |
|
strict; no coercion ( |
|
ordering; numeric for numbers, lexicographic for strings |
Use === by default. The only good case for == is
x == null (matches both null and undefined).
0 == false; // true
0 === false; // false
"" == 0; // true
null == undefined; // true
NaN === NaN; // false; use Number.isNaN
Logical#
&&, ||, and ?? short-circuit and return one of their
operands.
x && y; // x if x is falsy, else y
x || y; // x if x is truthy, else y
x ?? y; // x if x is non-null/undefined, else y
const port = config.port ?? 8080; // only nullish falls through
const host = config.host || "localhost"; // any falsy falls through
! returns a clean boolean; !!x is the operator’s coercion
shortcut.
Assignment#
= and its compound variants (+= -= *= /= %= **= &= |= ^=
<<= >>= >>>= &&= ||= ??=).
x = 1;
x += 2; // x = x + 2
x ??= 10; // x = x ?? 10 (assign only if nullish)
x ||= "default"; // x = x || "default"
Optional chaining#
?. short-circuits the chain when the left side is null
or undefined, returning undefined instead of throwing.
user?.profile?.name; // undefined if user or profile is null
user?.greet?.(); // undefined if greet is missing
user?.["profile"]?.["name"]; // computed-key variant
Spread / rest#
... spreads an iterable into individual elements or collects
remaining elements into an array.
Math.max(...[1, 2, 3]); // spread; same as Math.max(1, 2, 3)
const merged = {...a, ...b}; // shallow merge
const [first, ...rest] = arr; // rest in destructuring
function f(first, ...rest) {} // rest in parameters
Bitwise#
Operate on 32-bit signed integers; the operands are coerced from
double via ToInt32.
Operator |
Meaning |
|---|---|
|
AND, OR, XOR, NOT |
|
left shift, arithmetic right shift (sign-extending) |
|
logical right shift (zero-fill) |
0xff & 0x0f; // 15
1 << 8; // 256
-1 >>> 0; // 4294967295 (logical shift exposes unsigned)
Bigint operators#
The same arithmetic and bitwise operators work on bigint;
both operands must be bigint. / is integer (no remainder).
1n + 2n; // 3n
10n / 3n; // 3n
1n + 1; // TypeError
typeof / instanceof#
typeof x returns one of the type-tag strings. x instanceof
C is true if C.prototype appears in x’s prototype
chain.
typeof 42; // "number"
typeof "x"; // "string"
typeof undefined; // "undefined"
typeof null; // "object" (historic bug)
typeof fn; // "function"
[] instanceof Array; // true
new Date() instanceof Date; // true
Precedence#
A small subset, highest to lowest. The operator parenthesises anything ambiguous; the compiler does not warn.
Group |
Operator |
|---|---|
call / member |
|
call / member |
|
call / member |
|
call / member |
|
call / member |
|
unary |
|
unary |
|
unary |
|
unary |
|
unary |
|
unary |
|
unary |
|
unary |
|
unary |
|
unary |
|
exponent |
|
multiplicative |
|
multiplicative |
|
multiplicative |
|
additive |
|
additive |
|
shift |
|
shift |
|
shift |
|
relational |
|
relational |
|
relational |
|
relational |
|
relational |
|
relational |
|
equality |
|
equality |
|
equality |
|
equality |
|
bitwise |
|
bitwise |
|
bitwise |
|
logical |
|
logical |
|
logical |
|
conditional |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
assignment |
|
References#
Types for coercion rules and the falsy table.
Control flow for how operators feed conditions.