Operators
Operators are the built-in symbols that combine or transform values: arithmetic, comparison, logical, bitwise, assignment, and a handful of keyword operators such as typeof and in. Each one has a precedence, an associativity, and its own coercion rules.
Detailed Explanation
Operators come in three shapes by operand count. Unary takes one (-x, !flag, typeof x, await p). Binary takes two (a + b, a && b, key in obj). Ternary takes three, and there is exactly one: condition ? a : b.
The families worth knowing:
- Arithmetic:
+ - * / % **.%is remainder, not modulo, so it keeps the sign of the left operand:-7 % 3is-1.**is exponentiation, replacingMath.pow. - Assignment:
=plus the compound forms+= -= *= /= %= **=, the bitwise ones, and the logical ones||= &&= ??=. - Comparison:
< > <= >=compare numerically, or lexicographically when both sides are strings, which is why"10" < "9"istrue. - Equality:
===and!==compare without conversion.==and!=convert first and should be avoided, with the single idiomatic exception ofx == null, which istruefor bothnullandundefined. - Logical:
&&,||,??,!. The first three short-circuit and return one of their operands, not a boolean. - Bitwise:
& | ^ ~ << >> >>>. These coerce their operands to 32-bit integers, so anything above 2^31 − 1 wraps. - Keyword operators:
typeof,instanceof,in,delete,void,new,await,yield, plus the comma operator.
The one to understand deeply is +, because it is overloaded. If either operand is a string after primitive conversion, + concatenates; otherwise it adds. This is why 1 + "2" is "12" while 1 - "2" is -1, since - has no string meaning and coerces to number. Unary + is a compact numeric cast: +"42" is 42, and +new Date() is a timestamp.
That logical operators return operands rather than booleans is what makes const port = input || 3000 work — and what makes it buggy. || treats 0, "", NaN, null, undefined, and false as reasons to take the right side, so a legitimately-zero port becomes 3000. ?? was added in ES2020 to fall back only on null and undefined, which is what "no value provided" usually means.
Precedence decides grouping when you omit parentheses: ** binds tighter than *, which binds tighter than +, which binds tighter than comparison, then &&, then ||, then the ternary, then assignment. Most operators are left-associative; assignment and ** are right-associative, so 2 ** 3 ** 2 is 512. Rather than memorise the full table, parenthesise anything a reader would have to look up.
The costs are readability, not speed. Dense chains of ??, ?., and ternaries compress a lot of logic into one line that is hard to debug, and relying on coercion instead of converting explicitly hides intent from the next person.
Code Examples
`+` is overloaded; every other arithmetic operator coerces to number.
// `+` is overloaded; every other arithmetic operator coerces to number.
console.log(1 + "2"); // => 12 string concatenation wins
console.log(1 - "2"); // => -1 "-" has no string meaning
console.log("6" * "7"); // => 42 both coerced to numbers
console.log(+"42"); // => 42 unary + as a numeric cast
console.log([] + {}); // => [object Object] both become strings first
console.log(2 ** 3 ** 2); // => 512 ** is right-associative: 2 ** 9
console.log(-7 % 3); // => -1 remainder keeps the left signLogical operators return an operand, which matters for defaults.
// Logical operators return an operand, which matters for defaults.
function connect(port) {
const withOr = port || 3000; // falls back on ANY falsy value, including 0
const withNullish = port ?? 3000; // falls back only on null/undefined
return { withOr, withNullish };
}
console.log(connect(8080)); // => { withOr: 8080, withNullish: 8080 }
console.log(connect(0)); // => { withOr: 3000, withNullish: 0 } <- the bug and the fix
console.log(connect(undefined)); // => { withOr: 3000, withNullish: 3000 }
console.log("ana" && "admin"); // => admin the last truthy operand, not `true`Logical assignment operators, ES2021.
// Logical assignment operators, ES2021.
const settings = { theme: "", retries: 0 };
settings.theme ||= "dark"; // assigns when the current value is falsy
settings.retries ??= 5; // assigns only when null or undefined
settings.locale ??= "en-GB"; // missing property is undefined, so it is set
console.log(settings); // => { theme: 'dark', retries: 0, locale: 'en-GB' }
// Bitwise operators work on 32-bit integers.
console.log(5 & 3); // => 1
console.log(1 << 31); // => -2147483648 overflows into the sign bitCommon Gotchas
`??` cannot be mixed with `||` or `&&` unquoted
`a ?? b || c` is a `SyntaxError`. The committee made it explicit because the intended precedence is ambiguous to readers. Add parentheses: `(a ?? b) || c`.
`||` swallows valid falsy values
`count || 10` replaces a real `0`, and `name || "anon"` replaces a real empty string. Reach for `??` whenever the falsy values are legitimate data.
Comparison operators compare strings as text
`"10" < "9"` is `true` because `"1"` sorts before `"9"`. Convert with `Number()` before comparing anything that arrived from an input field or a URL.
Bitwise operators truncate to 32 bits
they are a poor fit for JavaScript's 64-bit doubles, so `2 ** 32 | 0` is `0`. Use `BigInt` operators when you need wider bit manipulation.