Arrays & objects · Output question
JavaScript object spread and repeated properties
New properties follow a spread in an object literal. Predict which values belong to the original and the new object.
const base = { ready: true, count: 1 };
const next = { ...base, count: 4, ready: false };
console.log(base.count);
console.log(next.count, next.ready);What gets logged?
Choose an output before opening the walkthrough, or check your answer in practice mode.
Reveal the answer and execution trace
1
4 falseProperties written after a spread replace earlier values on the newly created object. base is unchanged.
- Line 2. base contributes ready: true and count: 1 to a new object.
- Line 2. The later count and ready properties replace those two values on next.
- Line 3. base still reports a count of 1.
- Line 4. next reports 4 and false.
Reference: Spread syntax on MDN.