whatstheoutput_

#98: {...original} and a nested object

Topic: objects · Difficulty: ★★

const original = { info: { count: 1 } };
const copy = { ...original };
copy.info.count = 99;
console.log(original.info.count);
console.log(copy.info.count);

Output

99
99

Why

Object spread only copies the top level of an object: for each own enumerable property it copies the VALUE stored there. When that value is itself an object (like info), the value being copied is a reference — so copy.info and original.info end up pointing at the exact same inner object, not two separate ones. Mutating copy.info.count therefore also changes what original.info.count reads, because there was only ever one 'info' object shared between them.

More objects puzzles

Play today's puzzle →