Destructuring with Default
Predict Output Medium
Question
What does this code print?
Code
const { x = 10, y = 20 } = { x: 5 };
console.log(x + y);
Hint
x is provided (5), y uses its default (20).
Explanation
The object { x: 5 } has x but not y. Destructuring assigns x = 5 (from the object) and y = 20 (the default, since y is not in the object). x + y = 5 + 20 = 25. The output is: 25
Related Resources
Related Lessons
- Objects
Learn how objects work.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.