Object Spread
Predict Output Easy
Question
What does this code print?
Code
let a = {x: 1};
let b = {...a, y: 2};
console.log(b.x + b.y);
Hint
Spread copies properties from a into b.
Explanation
{...a, y: 2} creates a new object with all properties from a (x: 1) plus y: 2. So b is {x: 1, y: 2}. b.x is 1 and b.y is 2, sum is 3. This is a shallow copy — nested objects are shared by reference.
Related Resources
Related Lessons
- Objects
Learn JS objects.
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.