JavaScript ||= Logical OR assignment Operator
The logical OR assignment operator (||=) assigns a value to a variable only if the variable is currently falsy.
Description
The logical OR assignment operator x ||= y assigns y only if x is falsy. It's commonly used to set defaults, but because 0, '' and false are falsy, those valid values get replaced too. If you only want to replace null and undefined, use ??=.
Like the other logical assignment operators, it short-circuits: when the variable already has a truthy value, the right-hand side isn't evaluated and no assignment is made. That makes it a convenient way to fill in a value lazily, such as cache.data ||= computeData(), where the expensive function only runs the first time.
The equivalent long form is x || (x = y), not x = x || y; the difference is that no assignment happens when x is truthy, which matters for setters and frozen objects. Choose ||= when every falsy value should be replaced, and ??= when only missing values should.
Properties
- Operator
||=- Category
- Assignment
- Precedence
- 2 (1 is lowest, 18 is highest)
- Associativity
- right-to-left
Syntax
x ||= yExample
Browser Support
The following information will show you the current browser support for the ||= operator. Hover over a browser icon to see the version that first introduced support for this operator.
Desktop





Tablets & Mobile





