Operators
- Vocabulary of operand
Unary operators
A unary operator is applied to a single operand, such as the increment operator ++
which can be used as x++
or ++x
.
Math operators
- addition coerces to number
- adding bigints
Logic operators
- and, or, not
Bitwise operators
- and, or, xor, not
Nullish coalescence
The nullish coalescing operator ??
returns the right operand if the left operand is null.
A nullish coalescing operator can be used to "fall back" on a default value if the provided value is null
or undefined
. The ||
operator is also used to fall back on a default value if the left operand is falsy (i.e. 0
, ''
, and false
are all falsy and will cause the right operand to be produced).
Consider the example below, where a map stores the distance that each person is from a particular location. If we were to use the ||
operator instead of the nullish coalescence operator, the function would produce an unexpected Infinity
for the name "Carol"
where the distance is 0
(a falsy value).
Optional chaining
The optional chaining operator ?
accesses an object property, and returns undefined
if the value is undefined
or null
. This operator is often useful in accessing nested object properties that may be optional, or calling an object function that may not be defined.