does this API call get executed even if
myDatais 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 OverflowVideos
does this API call get executed even if
myDatais 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
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
The answer is no. details
The error means your version of Node does not yet have support for the ??= operator.
Check out the versions which support it on node.green:

It's possible for node version v15.14+.
The rewrite for something like
a.greeting ??= "hello"
in node <v15.14 is:
a.greeting = a?.greeting ?? 'hello'
Maybe it does help someone :]
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?
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"
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.