Start here.
Programs often need to describe a value that is unavailable. If an object has no property called age, reading person.age gives undefined. A function with no returned value also produces undefined. null is a value a programmer can put somewhere deliberately to mean there is no item here. Both represent absence, but they can tell different stories: one was not supplied, while the other was explicitly set to empty.
A default is a fallback value used when the original is unsuitable. The expression count ?? 10 means: use count unless it is null or undefined; otherwise use 10. The double question mark is the nullish coalescing operator. The expression count || 10 uses a broader rule. It treats any falsy value as a reason to use 10, including the perfectly valid count 0. In a settings form, an empty string or false may also be intentional and should often be preserved.
Read each side of a default expression as a possible result. With count equal to 0, 0 ?? 10 evaluates to 0 because zero is present. With count equal to null, null ?? 10 evaluates to 10. You may also meet optional chaining, written person?.name. It stops a property lookup and produces undefined when person is null or undefined. It solves a different problem from ??, although the two often appear together: person?.name ?? 'Guest'.
Build the model.
Undefined usually means no value has been supplied or found. Null is a deliberate empty value. Both signal absence, but they communicate different intentions. A missing property produces undefined; an API may explicitly return null to say that a search found nothing.
Use ?? when a default should replace only null or undefined. The || operator replaces every falsy value, including zero, false, and the empty string. Choosing between them is a small decision with visible consequences in forms, settings, and counters.
How it works.
Reading a property that is not present normally yields undefined. A function with no explicit return also yields undefined. null is a value a programmer can choose to state that there is deliberately no result. Neither one is the same as zero, false, or an empty string: those may be complete, useful answers in their own right.
A default is a policy, so choose its trigger carefully. The expression left ?? right uses right only when left is null or undefined. The expression left || right uses right whenever left is falsy. If a setting is allowed to be zero, an empty label, or false, || may silently erase the user's choice. Optional chaining, such as user?.name, can safely stop a property read when user is absent; it does not validate a present value's shape.
Read it line by line.
const count = 0;
console.log(count || 10); // 10
console.log(count ?? 10); // 0First notice. Zero is a real count, so nullish coalescing preserves it.
count is zero. For count || 10, JavaScript checks whether zero is truthy. It is not, so the expression yields 10. For count ?? 10, JavaScript asks whether zero is null or undefined. It is neither, so the expression yields zero. Both expressions are legal; they answer different questions.
See the rule again.
const savedVolume = 0;
const volume = savedVolume ?? 50;
console.log(volume);The result is 0. A missing setting would use 50, but a saved zero is a real instruction to mute. Replacing ?? with || would incorrectly turn mute into volume 50.
One thing to remember.
Absence is narrower than falsiness.