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

When do destructuring default values apply?

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

JavaScript
const { a = 1, b = 2 } = { a: null, b: undefined };
console.log(a, b);
Show answer and explanation

The answer is null 2.

Like default parameters, destructuring defaults only kick in for undefined. null is treated as a real value.

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. What is the difference between ?? and || in JavaScript?
  4. How does optional chaining short-circuit in JavaScript?
  5. How do you swap two variables with destructuring?

Related topics