Learning objective: Choose between ||, ?? and default parameters, and use ??= and ||= to fill in missing values without overwriting valid zeros and empty strings.
Skills practised: Nullish coalescing and defaults.
04Explanation
a || b returns b whenever a is falsy: false, 0, "", null, undefined or NaN.
a ?? b (nullish coalescing) returns b only when a is null or undefined. Zero, empty strings and false are kept.
Default parameters give a parameter a value when the argument is missing or undefined: function greet(name = "friend").
x ??= value assigns only if x is nullish; x ||= value assigns if x is falsy.
Choose by meaning: if 0 or "" is a valid value, use ??. If any falsy value should be replaced, use ||.
?? cannot be mixed directly with || or && without parentheses: a || b ?? c is a SyntaxError.
Default parameters are evaluated at call time, so function f(list = []) gets a fresh array on every call, and defaults can refer to earlier parameters.
Passing null does not trigger a default parameter; only undefined does.
05Definitions
Nullish coalescing
The ?? operator, which returns its right side only when the left side is null or undefined.
Falsy
A value treated as false in a condition: false, 0, -0, 0n, "", null, undefined and NaN.
Default parameter
A value used for a parameter when its argument is undefined.
Logical assignment
The ??=, ||= and &&= operators, which assign only under a condition.
ex-044-04Defaults can use earlier parameters Browser-executable JavaScript
Each default is evaluated when the function is called.
function range(start, end = start + 3, step = 1) {
const out = [];
for (let i = start; i < end; i += step) out.push(i);
return out;
}
console.log(range(2));
console.log(range(0, 10, 4));
Sample input
None
Expected output
[2, 3, 4]
[0, 4, 8]
Common mistake
Referring to a later parameter in a default, which throws a ReferenceError.
A media player stores volume, playback speed and captions. A volume of 0 and captions turned off are real choices, so the code uses ?? to fill only settings the user never chose.
3. Which argument makes a default parameter apply?
null
0
undefined
false
4. Why is this a SyntaxError?
const a = null, b = 0, c = 1;
console.log(a || b ?? c);
?? cannot be mixed with || without parentheses
a is undefined
c is reserved
?? needs a semicolon
5. What is printed?
const o = { n: 0 };
o.n ??= 1;
console.log(o);
{n: 0}
{n: 1}
{}
An error
6. A discount of 0% is valid. Which line is correct?
const d = input.discount || 10;
const d = input.discount ?? 10;
const d = input.discount && 10;
const d = !input.discount ? 10 : 0;
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Keep a zero quantity: Prints Pen x0; Uses ??
Greeting with defaults: Prints both; No arguments
Normalise options: Prints the options; No argument; Keeps true
ex-044-01: output matches the documented expected output
ex-044-02: output matches the documented expected output
ex-044-03: output matches the documented expected output
ex-044-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-09-modern-javascript-syntax/lesson-044-nullish-coalescing-and-default-values/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationnullish coalescing and default values
Context: I am learning nullish coalescing and default values in JavaScript.
Task: Explain || versus ?? and default parameters using a settings example with zeros and empty strings.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionnullish coalescing and default values drill
Context: I have practised nullish coalescing and default values.
Task: Give me six snippets mixing ||, ??, ??= and defaults and ask what each prints.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingnullish coalescing and default values bug
Context: My setting of 0 keeps being replaced by the default value.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationnullish coalescing and default values practice
Context: I finished the nullish coalescing and default values exercises.
Task: Create three exercises that normalise options objects.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewnullish coalescing and default values review
Context: Here is my code that uses nullish coalescing and default values.
Task: Review my code for || fallbacks that should be ?? and for defaults that miss null.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.