Update

JavaScript now supports the nullish coalescing operator (??). It returns its right-hand-side operand when its left-hand-side operand is null or undefined, and otherwise returns its left-hand-side operand.

Old Answer

Please check compatibility before using it.


The JavaScript equivalent of the C# null coalescing operator (??) is using a logical OR (||):

Copyvar whatIWant = someString || "Cookies!";

There are cases (clarified below) that the behaviour won't match that of C#, but this is the general, terse way of assigning default/alternative values in JavaScript.


Clarification

Regardless of the type of the first operand, if casting it to a Boolean results in false, the assignment will use the second operand. Beware of all the cases below:

Copyalert(Boolean(null)); // false
alert(Boolean(undefined)); // false
alert(Boolean(0)); // false
alert(Boolean("")); // false
alert(Boolean("false")); // true -- gotcha! :)

This means:

Copyvar whatIWant = null || new ShinyObject(); // is a new shiny object
var whatIWant = undefined || "well defined"; // is "well defined"
var whatIWant = 0 || 42; // is 42
var whatIWant = "" || "a million bucks"; // is "a million bucks"
var whatIWant = "false" || "no way"; // is "false"
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing_assignment
Nullish coalescing assignment (??=) - JavaScript | MDN
The nullish coalescing assignment (??=) operator, also known as the logical nullish assignment operator, only evaluates the right operand and assigns to the left if the left operand is nullish (null or undefined).
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Nullish coalescing operator '??'
The nullish coalescing operator ?? provides a short way to choose the first “defined” value from a list. It’s used to assign default values to variables:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
The nullish coalescing operator avoids this pitfall by only returning the second operand when the first one evaluates to either null or undefined (but no other falsy values):
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › nullish-coalescing-assignment-operator-in-javascript
Nullish Coalescing Assignment (??=) Operator in JavaScript - GeeksforGeeks
July 23, 2025 - Only if the value of x is nullish then the value of y will be assigned to x that means if the value of x is null or undefined then the value of y will be assigned to x.
Top answer
1 of 16
2331

Update

JavaScript now supports the nullish coalescing operator (??). It returns its right-hand-side operand when its left-hand-side operand is null or undefined, and otherwise returns its left-hand-side operand.

Old Answer

Please check compatibility before using it.


The JavaScript equivalent of the C# null coalescing operator (??) is using a logical OR (||):

Copyvar whatIWant = someString || "Cookies!";

There are cases (clarified below) that the behaviour won't match that of C#, but this is the general, terse way of assigning default/alternative values in JavaScript.


Clarification

Regardless of the type of the first operand, if casting it to a Boolean results in false, the assignment will use the second operand. Beware of all the cases below:

Copyalert(Boolean(null)); // false
alert(Boolean(undefined)); // false
alert(Boolean(0)); // false
alert(Boolean("")); // false
alert(Boolean("false")); // true -- gotcha! :)

This means:

Copyvar whatIWant = null || new ShinyObject(); // is a new shiny object
var whatIWant = undefined || "well defined"; // is "well defined"
var whatIWant = 0 || 42; // is 42
var whatIWant = "" || "a million bucks"; // is "a million bucks"
var whatIWant = "false" || "no way"; // is "false"
2 of 16
82
Copyfunction coalesce() {
    var len = arguments.length;
    for (var i=0; i<len; i++) {
        if (arguments[i] !== null && arguments[i] !== undefined) {
            return arguments[i];
        }
    }
    return null;
}

var xyz = {};
xyz.val = coalesce(null, undefined, xyz.val, 5);

// xyz.val now contains 5

this solution works like the SQL coalesce function, it accepts any number of arguments, and returns null if none of them have a value. It behaves like the C# ?? operator in the sense that "", false, and 0 are considered NOT NULL and therefore count as actual values. If you come from a .net background, this will be the most natural feeling solution.

🌐
Medium
habtesoft.medium.com › understanding-nullish-coalescing-assignment-in-javascript-649519a33bb8
Understanding Nullish Coalescing Assignment (??=) in JavaScript | by habtesoft | Medium
October 19, 2024 - The Nullish Coalescing Assignment (??=) is a relatively new operator in JavaScript, introduced in ES2021. It provides a shorthand way to assign a value to a variable only if the variable is null or undefined.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-advanced-operators
Advanced JavaScript Operators – Nullish Coalescing, Optional Chaining, and Destructuring Assignment
January 4, 2024 - When the firstName value is null or undefined, then the value Guest will be assigned to the username variable instead: ... As you can see, you don’t need an if-else statement to check for null or undefined values. The nullish coalescing operator was created as an improved alternative to the OR operator ||.
Find elsewhere
🌐
Medium
medium.com › @vinaychhabra.dev › nullish-coalescing-assignment-operator-in-javascript-fe55880a5384
Nullish Coalescing Assignment Operator in JavaScript | by Vinay Chhabra | Medium
April 9, 2024 - We’ve previously explored the ... introduced the Nullish Coalescing Assignment Operator (??=), a powerful addition to simplify default value assignments....
🌐
V8
v8.dev › features › nullish-coalescing
Nullish coalescing · V8
Default assignment inside object ... the assignment. But these strict equality tests for only undefined aren’t always desirable, and an object to perform destructing on isn’t always available. For instance, maybe you want to default on a function’s return values (no object to destructure). Or maybe the function returns null (which is common for DOM APIs). These are the times you want to reach for nullish coalescing...
🌐
Reddit
reddit.com › r/javascript › why doesn't javascript have a nullish-coalescing-assignment (??=) operator?
r/javascript on Reddit: Why doesn't JavaScript have a nullish-coalescing-assignment (??=) operator?
July 28, 2021 -

So, JavaScript has x ?? y, which favors the right-hand side if the left one is undefined or null.
However, an oddity that's present with this operator is it's assignment-form.
In JavaScript, most operators have an assignment form + has +=, - has -=, heck, even ** the exponential operator has an assignment-form, and you can probably guess what that looks like.

However, ??= is conveniently absent.
This isn't a huge deal, but it makes the language a little inconsistent, along with that, it would make as a small way to remove redundancy from class-constructors:

class Shape {
    constructor(x, y, w, h) {
        this.x = x ?? this.x;
        this.y = y ?? this.y;
        this.w = w ?? this.w;
        this.h = h ?? this.h;
    }
    // Class body...
}

// Could theoretically become...

class Shape {
    counstructor(x, y, w, h) {
        this.x ??= x;
        this.y ??= y;
        this.w ??= w;
        this.h ??= h;
    }
    // Class body....
}

And since small factorizations like this are the reason why even have +=, I pose to you a question identical to that of the one in the title:
Why do you think JavaScript doesn't have a nullish-coalescing assignment operator?

🌐
GitHub
github.com › mdn › content › blob › main › files › en-us › web › javascript › reference › operators › nullish_coalescing_assignment › index.md
content/files/en-us/web/javascript/reference/operators/nullish_coalescing_assignment/index.md at main · mdn/content
No assignment is performed if the left-hand side is not nullish, due to short-circuiting of the [nullish coalescing](/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing) operator.
Author   mdn
🌐
Medium
medium.com › @gabrielairiart.gi › advanced-javascript-use-of-nullish-coalescing-and-optional-chaining-and-for-efficient-coding-7d1d3fe3eedf
Advanced JavaScript: Use of Nullish Coalescing ?? and Optional Chaining and ?. for Efficient Coding | by Gabriela Iriart | Medium
March 22, 2024 - When juxtaposing Nullish Coalescing with Optional Chaining, it’s crucial to grasp that they fulfill distinct yet complementary roles. Nullish Coalescing ensures a variable is assigned a definitive value if it’s found to be null or undefined, thereby preventing the variable from remaining in an indeterminate state.
🌐
W3Schools
w3schools.com › jsref › jsref_oper_nullish.asp
JavaScript Nullish Coalescing Operator
cssText getPropertyPriority() getPropertyValue() item() length parentRule removeProperty() setProperty() JS Conversion ... The ?? operator returns the right operand when the left operand is nullish (null or undefined), otherwise it returns the left operand...
🌐
BetterBugs
betterbugs.io › blog › javascript-double-question-mark-nullish-coalescing-operator
What is JavaScript Double Question Mark (??) or the Nullish Coalescing Operator?
Here, the experienceYears property ... coalescing assignment operator “??=” is a powerful solution when you want only the null or undefined values to be considered as falsy....
🌐
CoreUI
coreui.io › answers › how-to-use-nullish-coalescing-in-javascript
How to use nullish coalescing in JavaScript · CoreUI
November 18, 2025 - The nullish coalescing operator provides a precise way to handle null and undefined values without affecting other falsy values like empty strings or zero. As the creator of CoreUI, a widely used open-source UI library, I’ve used nullish ...
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript nullish coalescing operator
JavaScript Nullish Coalescing Operator
September 1, 2008 - Let's undersand the nullish coalescing operator in details with the help of some examples. In the example below, the value of the x is null. We used the x as the first operand and 5 as the second. You can see in the output that the value of y is 5, as x is null. You can assign undefined to ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-nullish-coalescing-operator
JavaScript Nullish Coalescing(??) Operator - GeeksforGeeks
The nullish coalescing (??) operator is used to handle null and undefined values in JavaScript. It allows you to assign a default value when a variable does not have a valid value.
Published   March 2, 2020