Nullish Coalescing with ??
Provide defaults only for null and undefined.
Nullish Coalescing with ?? is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Providing Defaults
You often want a fallback when a value is missing. The nullish coalescing operator gives a clean way to supply defaults only when needed.
const name = undefined;
console.log(name ?? "Guest"); // "Guest"The ?? Operator
The double-question-mark operator returns the right-hand value only when the left side is null or undefined.
console.log(null ?? "fallback"); // "fallback"
console.log(undefined ?? "fallback"); // "fallback"
console.log("hi" ?? "fallback"); // "hi"Why Not Use ||
The logical OR || falls back on any falsy value, including 0, "", and false, which is often wrong for real data.
console.log(0 || 10); // 10 (probably unwanted)
console.log(0 ?? 10); // 0 (keeps the real value)Empty String Case
With || an intentional empty string is replaced; with ?? it is kept because "" is not nullish.
const note = "";
console.log(note || "none"); // "none"
console.log(note ?? "none"); // ""false Is Preserved
A real false value survives ?? but would be overridden by ||.
const enabled = false;
console.log(enabled ?? true); // false
console.log(enabled || true); // trueReading Config Safely
?? is ideal for settings where 0 or empty are valid choices a user might intentionally pick.
function getVolume(settings) {
return settings.volume ?? 50;
}
console.log(getVolume({ volume: 0 })); // 0Works With Variables
It works with any expressions, returning the first operand that is not nullish.
let cached = null;
const value = cached ?? computeValue();
function computeValue() { return 99; }
console.log(value); // 99Short-circuit Evaluation
Like || and &&, ?? short-circuits: if the left side is not nullish, the right side is never evaluated.
function side() { console.log("called"); return 1; }
const x = "ready" ?? side(); // side() never runs
console.log(x);Cannot Mix With && or || Directly
Mixing ?? with && or || without parentheses is a syntax error, to avoid ambiguity.
const a = true, b = null;
// const c = a || b ?? 1; // SyntaxError
const c = (a || b) ?? 1;
console.log(c);Default Function Parameters vs ??
Default parameters only apply when an argument is undefined. ?? also handles null passed explicitly.
function greet(name = "Guest") { return "Hi " + name; }
console.log(greet(null)); // "Hi null"
function greet2(name) { return "Hi " + (name ?? "Guest"); }
console.log(greet2(null)); // "Hi Guest"When to Choose Which
Use ?? when 0, "", or false are valid values you must keep. Use || only when any falsy value should fall back.
const retries = 0;
console.log(retries ?? 3); // 0 keptQuick Check
Compare the operators.
Recap
The ?? operator returns the right side only when the left is null or undefined, preserving 0, "", and false. This makes it safer than || for defaults on real data. It short-circuits and cannot be mixed with &&/|| without parentheses.
Frequently asked questions
Is the “Nullish Coalescing with ??” lesson free?
Yes — the full text of “Nullish Coalescing with ??” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Nullish Coalescing with ??”?
Provide defaults only for null and undefined. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nullish Coalescing with ??” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Optional Chaining with the ?. Operator
- Nullish Coalescing with ??
- Combining ?. and ??
- Optional Calls and Index Access