does this API call get executed even if myData is non-null?

No, it will not be executed.

myData ??= await fetch("API")

is equivalent to

myData ?? (myData = await fetch("API"))

So, only if the first expression myData is nullish, the second assignment part runs.

let value = "test"
value ??= null.toString() // doesn't throw error because it is not executed
console.log(value)

value = null
value ??= null.toString() // throws error

Answer from shreesha adiga on Stack Overflow
🌐
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).
🌐
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.
🌐
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:
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › null-coalescing-operator
?? and ??= operators - null-coalescing operators - C# reference | Microsoft Learn
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....
🌐
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.
🌐
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.
🌐
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...
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 ||.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 48473
Nullish coalescing assignment operator doesn't work correctly with logical operators · Issue #48473 · microsoft/TypeScript
January 12, 2022 - Bug Report Using the nullish coalescing assignment operator ??= on a logical statement which contains a logical operator (&& or ||) fails to narrow the type, even though using the nullish c...
Published   Mar 29, 2022
🌐
Wikipedia
en.wikipedia.org › wiki › Null_coalescing_operator
Null coalescing operator - Wikipedia
October 31, 2025 - In earlier versions, it could be used via a Babel plugin, and in TypeScript. It evaluates its left-hand operand and, if the result value is not "nullish" (null or undefined), takes that value as its result; otherwise, it evaluates the right-hand operand and takes the resulting value as its result.
🌐
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....
🌐
TypeScript
typescriptlang.org › play › 3-7 › syntax-and-messaging › nullish-coalescing.ts.html
TypeScript: Playground Example - Nullish Coalescing
-1; config.active = config.active ?? true; // Current solution config.name = typeof config.name === "string" ? config.name : "(no name)"; config.items = typeof config.items === "number" ? config.items : -1; config.active = typeof config.active === "boolean" ? config.active : true; // Using || operator which could give bad data config.name = config.name || "(no name)"; // does not allow for "" input config.items = config.items || -1; // does not allow for 0 input config.active = config.active || true; // really bad, always true } // You can read more about nullish coalescing in the 3.7 blog post: https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/
🌐
TypeScript ESlint
typescript-eslint.io › rules › prefer-nullish-coalescing
prefer-nullish-coalescing | typescript-eslint
Enforce using the nullish coalescing operator instead of logical assignments or chaining.
🌐
LinkedIn
linkedin.com › pulse › nullish-coalescing-assignment-rolazar-web-developer
Nullish Coalescing Assignment
December 14, 2023 - The nullish coalescing operator can be combined with the assignment operator, so instead of writing JavaScript code this: The nullish coalescing assignment operator can be used like this: First it evaluates myValue.
🌐
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....
🌐
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?

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.