What gets logged?
const { x = 5 } = { x: null };
console.log(x);
- 5
- null
- undefined
- TypeError
0
voters
Yoshiii
What gets logged?
const { x = 5 } = { x: null };
console.log(x);
Yoshiii
This one’s a classic “you thought it was empty but JS didn’t” trap: it logs null.
Destructuring defaults only apply when the value is undefined, so { x: null } keeps x as null instead of falling back to 5. null is basically “I meant to put nothing here,” not “missing.”
Destructuring defaults don’t rescue you from null — they only kick in when the property is actually undefined, so this logs null.
{ x: null } is “x exists and it’s intentionally empty,” whereas {} (or { x: undefined }) is “x is missing,” which is when the = 5 default applies.
:: Copyright KIRUPA 2024 //--