JavaScript ??= Nullish coalescing assignment Operator
The nullish coalescing assignment operator (??=) assigns a value to a variable only if the variable is null or undefined.
Description
The nullish coalescing assignment operator x ??= y assigns y to x only when x is null or undefined. Other falsy values, such as 0 and '', are kept. It's the safest way to fill in missing defaults, and to create something on first use: cache[key] ??= [].
It short-circuits like the other logical assignments: the right-hand side is only evaluated, and the assignment only happens, when the current value is nullish. That makes obj.list ??= [] a concise way to create a property on first use and then reuse it, as in grouping items into arrays.
It's especially useful for function options, filling in defaults for missing settings without overwriting legitimate values such as 0, false or an empty string. Default parameters, function f(size = 10), only replace undefined, so ??= is handy when null should be replaced too.
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





