0Pricing
JavaScript Academy · Lesson

Nullish Coalescing with ??

Provide defaults only for null and undefined.

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"

All lessons in this course

  1. Optional Chaining with the ?. Operator
  2. Nullish Coalescing with ??
  3. Combining ?. and ??
  4. Optional Calls and Index Access
← Back to JavaScript Academy