EasyArray & Object Mutations🐛 Debug Challenge

Assignment copies reference — both variables point to same array

Buggy Code — Can you spot the issue?

const original = [3, 1, 2];
const sorted = original;
sorted.sort();
console.log(original.join(','));
console.log(sorted.join(','));

Fixed Code

const original = [3, 1, 2];
const sorted = [...original];
sorted.sort();
console.log(original.join(','));
console.log(sorted.join(','));

Bug Explained

Bug: = for arrays copies the reference, not the data. sorted and original point to the same array. Sorting one sorts both.

Explanation: Spread [...original] creates a new independent array. Sorting sorted no longer affects original.

Key Insight: = for arrays copies the reference. Use [...arr] or Array.from(arr) to create an independent copy.

More Array & Object Mutations Debug Challenges

Mediummap returns new array but objects inside are still referencesMediumObject.assign first argument is mutatedMediumSpread is shallow — nested array is still shared

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab