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"
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_oper_nullish.asp
JavaScript Nullish Coalescing Operator
The ?? operator returns the right operand when the left operand is nullish (null or undefined), otherwise it returns the left operand. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
๐ŸŒ
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):
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.

๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ javascript fundamentals
Nullish coalescing operator '??'
For example, here we show user if its value isnโ€™t null/undefined, otherwise Anonymous: let user; alert(user ?? "Anonymous"); // Anonymous (user is undefined) Hereโ€™s the example with user assigned to a name:
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-advanced-operators
Advanced JavaScript Operators โ€“ Nullish Coalescing, Optional Chaining, and Destructuring Assignment
January 4, 2024 - Hi Everyone! In this article, I'm going to teach you how to use three advanced JavaScript operators: the Nullish Coalescing, Optional Chaining, and Destructuring Assignment operators. These three operators will help you write clearer and less error-p...
๐ŸŒ
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....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript-nullish-coalescing-operator
JavaScript Nullish Coalescing(??) Operator - GeeksforGeeks
The nullish coalescing (??) operator is a logical operator in TypeScript that returns its right-hand side operand if the left-hand side operand is null or undefined; otherwise, it returns the left-hand side operand.
Published ย  June 10, 2024
Find elsewhere
๐ŸŒ
BetterBugs
betterbugs.io โ€บ blog โ€บ javascript-double-question-mark-nullish-coalescing-operator
What is JavaScript Double Question Mark (??) or the Nullish Coalescing Operator?
Hereโ€™s how you use a nullish coalescing operator (??): ... With the above code, if result1 is either null or undefined, finalResult would be assigned the value of result2.
๐ŸŒ
OpenReplay
blog.openreplay.com โ€บ mastering-javascript-optional-chaining-and-nullish-coalescing
Mastering JavaScript: optional chaining and nullish coalescing
In this example, the displayName variable is assigned the value of defaultName because the name variable is null. If the name variable had been undefined, the displayName variable would also have been assigned the value of defaultName. One thing to note is that the Nullish Coalescing operator only considers null and undefined as falsy values.
๐ŸŒ
W3Schools
w3schools.io โ€บ javascript โ€บ es11-nullish-coalescing-operator
ES2020(ES11) - Javascript Nullish Coalescing Operator - w3schools
so id always has value either valid value or default fallback value. To avoid this length code check, the nullish coalescing operator simplified this
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ nullish-coalescing-assignment-operator-in-javascript
Nullish Coalescing Assignment (??=) Operator in JavaScript - GeeksforGeeks
July 23, 2025 - let x = { name : "Ram" } // The value of name will remain // unchanged because x.name is not nullish x.name ??= "Shyam"; // There is no any property named age in object x . // So the value of x.age will be // undefined and undefined means nullish.
๐ŸŒ
Medium
habtesoft.medium.com โ€บ understanding-nullish-coalescing-assignment-in-javascript-649519a33bb8
Understanding Nullish Coalescing Assignment (??=) in JavaScript | by habtesoft | Medium
October 19, 2024 - Before diving into the assignment operator, itโ€™s important to understand the concept of nullish coalescing. The Nullish Coalescing Operator (??) allows you to return the right-hand operand if the left-hand operand is null or undefined.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ javascript โ€บ javascript nullish coalescing operator
JavaScript Nullish Coalescing Operator
September 1, 2008 - 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 the variable.
๐ŸŒ
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 combines this functionality with assignment, allowing for a more concise way to set a variable only if itโ€™s currently null or undefined. Before the introduction of ??=, you might have seen code that used the logical OR operator (||) to set default values. However, the logical OR operator treats falsy values (like 0, "", or false) as needing a replacement, which can lead to unintended results. The nullish coalescing operator, on the other hand, is focused only on null and undefined, making it more precise.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_2020.asp
JavaScript 2020
If the first value is false, the second value is assigned. ... The Nullish Coalescing Assignment Operator is used between two values.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ javascript โ€บ javascript nullish coalescing operator
What is a "null coalescing" operator in JavaScript?
September 1, 2008 - 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 the variable.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ what-is-the-nullish-coalescing-operator-in-javascript-and-how-is-it-useful
What is the Nullish Coalescing Operator in JavaScript, and how is it useful
May 5, 2023 - Using the nullish operator, we have the first operand as expression1(), and the second operand as expression2() and assign the value from the expression to result.
๐ŸŒ
Javascript-tutorial
javascript-tutorial.com โ€บ operators โ€บ nullish-coalescing-operator
Nullish coalescing operator (??) - The complete JavaScript Tutorial
Use the Nullish coalescing operator (??) to provide a fallback value in case the variable in question is NULL or undefined. As an alternative, you can use the logical OR operator (||), which will use the fallback value for anything that can be considered false, including NaN, 0, empty text strings and so on. If you're just looking to assign a value to a variable in case the variable is NULL or undefined, you can use the Nullish coalescing assignment operator (??=)....