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
Nullish coalescing operator (??) - JavaScript | MDN
Nullish coalescing assignment (??=) Optional chaining (?.) Logical OR (||) Default parameters ยท Was this page helpful to you? Yes ยท No Learn how to contribute ยท This page was last modified on Aug 26, 2025 by MDN contributors.
Discussions

javascript - Will the Logical nullish Assignment or nullish coalescing evaluate the assigned value in a non null case? - Stack Overflow
Does using nullish coalescing instead make a difference, for such a scenario ? I'm aware that I can use several methods including if-else for such a case, however I want to understand if these operators will for in such a scenario. ... No, it will not be executed. ... So, only if the first expression myData is nullish, the second assignment ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Nullish coalescing assignment operator (??=) in NodeJS - Stack Overflow
The node version you are using doesn't support the nullish coalescing assignment operator. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why doesn't JavaScript have a nullish-coalescing-assignment (??=) operator?
Part of ES2021: https://github.com/tc39/proposal-logical-assignment More on reddit.com
๐ŸŒ r/javascript
7
2
July 31, 2021
Thoughts on the Null Coalescing (??) operator precedence?
When something relatively new comes out, it's common for the first few games in town to foul up the artistry. Wirth is a towering intellect in this field, but he screwed up the precedence tables in Pascal. Experience will highlight mistakes, and then it's eventually time to design a new language. ?? clearly goes after function-call and field-access, but before arithmetic. The field-access counterpart .? should be on the same level as non-null field-access .. More on reddit.com
๐ŸŒ r/ProgrammingLanguages
11
31
April 30, 2024
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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ nullish-coalescing-assignment-operator-in-javascript
Nullish Coalescing Assignment (??=) Operator in JavaScript - GeeksforGeeks
July 23, 2025 - Firstly we all know that logical nullish assignment is represented as x ??= y, this is derived by two operators nullish coalescing operator and assignment operator we can also write it as x ?? (x = y). Now javascript checks the x first, if it ...
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ javascript fundamentals
Nullish coalescing operator '??'
April 23, 2023 - 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:
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_assignment.asp
JavaScript Assignment
If the first value is false, the ... ||= operator is an ES2020 feature. The Nullish coalescing assignment operator is used between two values....
Find elsewhere
๐ŸŒ
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 ||.
๐ŸŒ
TypeScript
typescriptlang.org โ€บ play โ€บ 3-7 โ€บ syntax-and-messaging โ€บ nullish-coalescing.ts.html
TypeScript: Playground Example - Nullish Coalescing
) { // With null-coalescing operator config.name = config.name ?? "(no name)"; config.items = config.items ?? -1; config.active = config.active ?? true; // Current solution config.name = typeof config.name === "string" ? config.name : "(no name)"; config.items = typeof config.items === "number" ?
๐ŸŒ
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....
๐ŸŒ
Trevor Lasn
trevorlasn.com โ€บ home โ€บ blog โ€บ javascript's ??= operator: default values made simple
JavaScript's ??= Operator: Default Values Made Simple
November 5, 2024 - If it is null or undefined, only then does it assign 'Anonymous' โ€” If user.name already has any other value - even an empty string or 0 - it stays untouched. JavaScript Operators: โ€™||โ€™ vs โ€™&&โ€™ vs โ€™??โ€™
๐ŸŒ
YouTube
youtube.com โ€บ watch
nullish coalescing assignment operator - YouTube
Closely related to the nullish coalescing operator, the nullish coalescing assignment operator lets us quickly assign new values to variables and object prop...
Published ย  April 17, 2023
Views ย  1K
๐ŸŒ
Hashnode
mohamedzhioua.hashnode.dev โ€บ understanding-nullish-coalescing-assignment-in-javascript
Understanding Nullish Coalescing Assignment (??=) in JavaScript
October 17, 2024 - With nullish coalescing assignment, you can assign a value to a variable only if its current value is either null or undefined (also known as "nullish" values).
๐ŸŒ
Hashnode
darshitanjaria.hashnode.dev โ€บ mastering-javascript-understanding-and-using-the-null-coalescing-operator
Unlock Cleaner JavaScript: Master the Null Coalescing Operator Today
April 18, 2025 - The Nullish Coalescing Assignment operator (??=) is a valuable addition to JavaScript, allowing developers to write cleaner, more concise code when dealing with default values. By focusing solely on null and undefined, it avoids the pitfalls ...
๐ŸŒ
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 31, 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?

๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ nullish-coalescing-assignment-rolazar-web-developer
Nullish Coalescing Assignment
December 14, 2023 - The nullish coalescing assignment operator can be used like this: let myValue = getValue(); myValue ??= "default value"; First it evaluates myValue. If myValue is not null or undefined, the remaining statement is ignored because of the nullish ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Learn JavaScript Nullish Coalescing Operator & Assignment ...
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ csharp โ€บ language-reference โ€บ operators โ€บ null-coalescing-operator
?? and ??= operators - null-coalescing operators - C# reference | Microsoft Learn
January 24, 2026 - The ?? operator doesn't evaluate ... null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null....