??= is similar to Nullish Coalescing Operator introduced as Logical nullish assignment implemented like other languages: php example. The logical nullish assignment is introduced in ES12 (ECMAScript 2022).

The logical nullish assignment (x ??= y) operator only assigns if x is nullish (null or undefined).

Example:

let foo = null;
let bar = "Give me a beer!";

foo ??= bar;

foo ?? (foo = bar) is the equivalent of the above expression. The variable foo gets assigned the variable bar only if foo is null or undefined. In this example, foo becomes "Give me a beer!". If we replace let foo = null with `let foo = "I don't need a beer!", the assignment does not happen. The variable foo stays "I don't need a beer!".

The equivalent code with an if statement is as follows:

let foo = null;
let bar = "Give me a beer!";

if (foo == null || foo == undefined) {
    foo = bar;
}

It does not check for other falsy values. It only checks for null and undefined.


So, in your example of code o[k] assigns an empty object {} when o[k] value results null or undefined.

Answer from Bhojendra Rauniyar on Stack Overflow
🌐
Flexiple
flexiple.com › javascript › double-question-mark-javascript
The Double Question Mark (??) in JavaScript - Flexiple
If the left-hand operand has a value other than null or undefined, it returns that value instead. Here's a simple usage example: let userColor = null; let defaultColor = "blue"; let currentColor = userColor ??
🌐
CoreUI
coreui.io › blog › what-is-double-question-mark-in-javascript
What is Double Question Mark in JavaScript? · CoreUI
March 12, 2024 - In this example, optional chaining (?.) safely navigates through the object. If any part of the chain is undefined, it evaluates to undefined, and the nullish coalescing operator then provides a default value. The double question mark in JavaScript is more than just syntactic sugar; it’s ...
🌐
sebhastian
sebhastian.com › javascript-double-question-mark
Learning JavaScript double question mark (??) or the Nullish Coalescing Operator | sebhastian
January 23, 2022 - For safety reasons, the double question mark can’t be used together with JavaScript OR (||) and AND (&&) operators without parentheses () separating the operators. For example, the following code tries to see if either firstName or lastName variable can be used as the value of username before using "Guest" as its value:
🌐
Medium
medium.com › @danialashrafk20sw037 › javascript-double-question-mark-57ebb6c6be55
JavaScript Double Question Mark. Introduction to JavaScript Double… | by Danialashrafksw | Medium
March 8, 2024 - The double question mark (`??`) ... falsy values like empty strings or zeros. ```javascript const defaultValue = 'Default'; const value = null; const result = value ??...
🌐
EyeUniversal
eyeuniversal.com › home › tech › deep dive into javascript's double question mark operator
Mastering JavaScript's Double Question Mark Operator
July 23, 2024 - Examples of operators in JavaScript include arithmetic operators such as +, -, *, and /, assignment operators like =, comparison operators like == and !=, logical operators like && and ||, and many more. Double Question Mark Operator was introduced in ECMAScript 2020.
🌐
BetterBugs
betterbugs.io › blog › javascript-double-question-mark-nullish-coalescing-operator
What is JavaScript Double Question Mark (??) or the Nullish Coalescing Operator?
Just like the “OR” (||) and “AND” (&&) logical operators in JavaScript, the “??” or the double question mark short circuits the chain of logical evaluation.
🌐
Scaler
scaler.com › home › topics › what is a nullish coalescing operator or double question mark (??) in javascript?
What is JavaScript double question mark (??) - nullish coalescing operator - Scaler Topics
March 28, 2024 - In this example, we'll keep values other than null or undefined but provide default values instead. ... We initialized three variables to store the number, empty string, and null value; after using the javascript double question mark, we can see the default value as output only for the variable with the null value. Earlier, it was a common practice to use the logical OR operator || to assign a default value to a variable:
Find elsewhere
🌐
Eran Stiller
eranstiller.com › javascript-double-question-marks
What Is the JavaScript Double Question Marks (??) Operator?
September 24, 2023 - This JavaScript Double Question Marks operator kicks in to provide a default value when the left-hand side operand, or the first operand, turns out to be null or undefined. This is especially useful when dealing with variables that may not have been initialized. ... Below is an example that ...
🌐
Mayallo
mayallo.com › home › shorts › the double question mark (nullish coalescing operator) in javascript
The Double Question Mark (Nullish Coalescing Operator) in JavaScript | Mayallo
April 16, 2025 - As you see, you can use both operators ... code. For example, look at userInput2, it might be acceptable for you if your client provides you with an empty string "", in such a case, you should use the ??...
🌐
freeCodeCamp
freecodecamp.org › news › how-the-question-mark-works-in-javascript
How the Question Mark (?) Operator Works in JavaScript
February 3, 2021 - But if you use || to provide a ... (for example, '' or 0). Consider a scenario where a variable has the value of 0 or an empty string. If we use (||), it will be considered as undefined or NULL and return some default value that we have fixed. Instead of the logical OR (||) operator, you can use double question marks (??), or Nullish ...
🌐
GoLinuxCloud
golinuxcloud.com › home › javascript › what is double question mark in javascript? [solved]
What is Double Question Mark in JavaScript? [SOLVED] | GoLinuxCloud
December 19, 2022 - This means that it returns the first operand if it is not null or undefined, and otherwise returns the second operand. This can be useful for providing default values for variables that may be null or undefined.
🌐
CyberITHub
cyberithub.com › how-does-javascript-double-question-mark-operator-works
How does JavaScript double question mark (??) operator works | CyberITHub
October 16, 2022 - Here’s an example below with a visitor assigned to a name:- let visitor = "Paul"; alert(visitor ?? "Anonymous"); // Paul (user defined) ... Nullish coalescing operators are very useful in Javascript programming language.
🌐
SkillReactor Blog
skillreactor.io › home › double question mark javascript operator explained
Double Question Mark JavaScript Operator Explained - SkillReactor Blog
July 8, 2024 - In the example above, the value variable is assigned a null value. However, the double question mark operator provides the fallback value ‘Default Value’ as the output. This ensures that a suitable alternative is used in cases where null or undefined values are encountered. The double question mark operator works seamlessly with optional chaining, another powerful feature in modern JavaScript.
🌐
MSR
rajamsr.com › home › double question mark in javascript (??): a comprehensive guide
Double Question Mark In JavaScript (??): A Comprehensive Guide | MSR - Web Dev Simplified
March 3, 2024 - In this example, the variable name is an empty string. When we use the double pipe operator, it evaluates the name as a false value. Since it is false, it proceeds to evaluate the next expression (defaultName) and assigns it to the result. The double question mark ?? and double pipe || are both used in JavaScript for handling default values, but they have some key differences in their behavior.
🌐
Enterprise DNA
blog.enterprisedna.co › javascript-double-question-mark
JavaScript Double Question Mark (??): What is it? – Master Data Skills + AI
To avoid such issues, always enclose your expressions with parentheses when mixing these operators: ... Now, the result will correctly be set to true. The JavaScript double question mark (??) operator offers a powerful and elegant solution for handling null and undefined values in your code.
🌐
Plain English
plainenglish.io › blog › javascript-operator
The JavaScript ?? (Nullish Coalescing) Operator: How Does it Work?
Whenever you use the nullish coalescing operator an empty string like “” or a 0 will not trigger the default value. As said above, null and undefined are the only two cases that will prompt the use of the default value when you use JavaScript double question mark.
🌐
vteams
vteams.com › blog › what-is-question-mark-in-javascript
Javascript's Double Question Mark: Meaning and Usage
April 23, 2024 - For instance, if you encounter a string with one value i.e. 0 or even an empty string. Then using the || operator this function will call the default value. Question mark Javascript can be used to fix this error. javascript double question mark “??” are used instead of || and it is also called the Nullish Coalescing...
🌐
Codedamn
codedamn.com › news › javascript
Double question mark in JavaScript (Nullish coalescing operator)
January 5, 2023 - 'Anonymous';Code language: JavaScript (javascript) It works just like the logical OR operator in this case, but it only assigns the default value of ‘Anonymous’ if usernameInput is null or undefined. If usernameInput is any other falsy value (such as 0 or “”), the nullish coalescing operator will not provide a default value. This might not seem like a big difference at first glance, but consider the following example:
🌐
Altcademy
altcademy.com › blog › what-is-double-question-mark-in-javascript
What is double question mark in JavaScript
September 17, 2023 - The ?? operator can be particularly useful in situations where you want to provide default values for variables. For example, consider this function that greets a user: