MediumArray & Object Mutations🐛 Debug Challenge

Spread is shallow — nested array is still shared

Buggy Code — Can you spot the issue?

const original = { name: 'Alice', scores: [90, 85] };
const copy = { ...original };

copy.scores.push(95);

console.log(original.scores.length);
console.log(copy.scores.length);

Fixed Code

const original = { name: 'Alice', scores: [90, 85] };
const copy = { ...original, scores: [...original.scores] };

copy.scores.push(95);

console.log(original.scores.length);
console.log(copy.scores.length);

Bug Explained

Bug: Spread creates a shallow copy. copy.scores and original.scores are the same array reference. Pushing to one pushes to both.

Explanation: Explicitly spreading the nested array creates a new independent copy of scores.

Key Insight: Spread is one level deep. Always explicitly spread nested arrays/objects that you plan to mutate independently.

More Array & Object Mutations Debug Challenges

EasyAssignment copies reference — both variables point to same arrayMediummap returns new array but objects inside are still referencesMediumObject.assign first argument is mutated

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab