Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

What is the difference between ?? and || in JavaScript?

Do you know what will be the output of this code?

JavaScript
console.log(0 ?? 5, 0 || 5);
Show answer and explanation

The answer is 0 5.

?? (nullish coalescing) only uses the right side when the left is null or undefined. || uses it for any falsy value, so 0 is replaced. Use ?? for defaults where 0 or "" are valid.

Take the full quiz Next question →

More ES6+ questions

  1. Does the spread operator make a deep copy?
  2. When do default parameters apply in JavaScript?
  3. How does optional chaining short-circuit in JavaScript?
  4. When do destructuring default values apply?
  5. How do you swap two variables with destructuring?

Related topics