codecks.

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.

  1. 4
    4 false
  2. 1
    4 false
  3. 1
    1 true
  4. 1
    4 true
Reveal the answer and execution trace
1
4 false

Properties written after a spread replace earlier values on the newly created object. base is unchanged.

  1. Line 2. base contributes ready: true and count: 1 to a new object.
  2. Line 2. The later count and ready properties replace those two values on next.
  3. Line 3. base still reports a count of 1.
  4. Line 4. next reports 4 and false.

Reference: Spread syntax on MDN.