Nullish Coalescing with ??
Provide defaults only for null and undefined.
Nullish Coalescing with ?? is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.
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.
Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Nullish Coalescing with ??” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Nullish Coalescing with ??”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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