Nullish Coalescing Operator (??)
πΉ What is ?? ?β
??is called the Nullish Coalescing Operator in JavaScript.- It returns the right-hand side value only when the left-hand side value is
nullorundefined. - If the left-hand side has any other falsy value (like
0,false,''), it does not replace it (unlike||).
πΉ Why use it?β
It helps us set default values safely, without mistakenly treating 0, false, or "" as empty.
πΉ Basic Syntaxβ
let result = value1 ?? value2;
π If value1 is not null/undefined, result = value1.
π If value1 is null/undefined, result = value2.
π Real-Life Examples (Basic β Realistic)
1. Default user nameβ
let username = null;
let displayName = username ?? "Guest";
console.log(displayName); // "Guest"
2. Difference with ||β
let score = 0;
console.log(score || 100); // 100 (wrong if 0 is valid)
console.log(score ?? 100); // 0 (correct)
3. Checking API responseβ
let apiResponse = undefined;
let data = apiResponse ?? "No data found";
console.log(data); // "No data found"
4. Default price in a shopping cartβ
let price = 0;
let finalPrice = price ?? 100;
console.log(finalPrice); // 0 β
(because 0 is valid)
5. Boolean flagsβ
let isActive = false;
let status = isActive ?? true;
console.log(status); // false β
(does not overwrite with true)
6. Nested objects (safe fallback)β
let settings = { theme: null };
let theme = settings.theme ?? "light";
console.log(theme); // "light"
7. Database default valuesβ
let userAge = undefined;
let ageToSave = userAge ?? 18;
console.log(ageToSave); // 18
8. Multiple fallbacksβ
let city = null;
let defaultCity = city ?? "New York" ?? "London";
console.log(defaultCity); // "New York"
9. Function parametersβ
function greet(name) {
let user = name ?? "Stranger";
console.log("Hello " + user);
}
greet(); // "Hello Stranger"
greet("Karthik"); // "Hello Karthik"
10. Real-time API config exampleβ
let config = { retries: 0 };
let retries = config.retries ?? 3;
console.log(retries); // 0 β
(valid, not replaced with 3)
β‘ Key Difference: ?? vs ||
||checks for falsy values (false, 0, '', null, undefined, NaN).??checks for only nullish values (null, undefined).
π Use ?? when 0, false, "" are valid values.
π Use || when you want to replace all falsy values.
π― 50 Interview Questions on ??
πΉ Beginner (1β15)β
- What is the nullish coalescing operator in JavaScript?
- Which values are considered nullish?
- How is
??different from||? - What does
let x = null ?? "default"return? - What does
let x = 0 ?? 100return? - Can
??replace the ternary operator in some cases? - Is
false ?? trueequal totrueorfalse? - Why was
??introduced in ES2020? - What will
let result = "" ?? "empty"return? - Can
??be chained multiple times? - What does
undefined ?? nullreturn? - Can you use
??with function parameters? - Why is
??safer than||in user input validation? - Will
NaN ?? 100returnNaNor100? - Which is better for defaults:
??or||?
πΉ Intermediate (16β35)β
- What is the result of
null ?? undefined ?? "hello"? - Why doesnβt
??considerfalseas nullish? - Can
??be used with objects? Give example. - Can
??be used in template literals? - What happens in
0 || 5vs0 ?? 5? - What is the output of
let a; console.log(a ?? "default")? - Whatβs the difference between
a = a ?? 1anda ||= 1? - Can
??be mixed with||in the same expression? - What is operator precedence of
??compared to||? - Can you use
??inside an arrow function default value? - Is
x = null ?? falseequal tofalseornull? - Why is
??not the same asa !== null ? a : b? - Does
null ?? 0return0ornull? - What happens if both sides of
??are nullish? - Can
??be used in database config defaults? - Does
??short-circuit evaluation? - Can
??be polyfilled for older browsers? - Compare
??vs optional chaining (?.). - Is
value ??= 10valid syntax? - When should you avoid using
???
πΉ Advanced (36β50)β
- Explain the internal algorithm of how
??evaluates operands. - Why canβt
??be mixed with||or&&without parentheses? - Whatβs the precedence of
??compared to?:(ternary)? - How does
??behave with BigInt values? - Does
??work with Symbol type? - How does
??interact with Proxy objects? - Can you use
??in default destructuring values? (e.g.,{ x = val ?? 5 }) - How does Babel transpile
??for older browsers? - What is the performance impact of
??in large loops? - Compare
??withif (value == null) value = default. - How does TypeScript handle
??differently? - What will
[null, undefined, 0].map(x => x ?? 5)return? - How does
??work inside JSON.parse defaults? - Can
??be overloaded in custom classes (via valueOf)? - Give a real-world case where
||fails but??works.
Got it π Hereβs the comparison table recreated in plain text (easy to copy without formatting issues).