EasyModern JS🐛 Debug Challenge

Manual swap loses original value

Buggy Code — Can you spot the issue?

let a = 1;
let b = 2;
a = b;
b = a;
console.log(a, b);

Fixed Code

let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b);

Bug Explained

Bug: a = b makes a = 2. Then b = a reads the already-changed a (2) — so b becomes 2. The original value of a (1) is lost.

Explanation: Destructuring swap evaluates the right side first as a snapshot [2, 1], then assigns both simultaneously.

Key Insight: Destructuring swap is atomic — values are captured before assignment. Sequential swap loses the original without a temp variable.

More Modern JS Debug Challenges

Easysort() mutates the original arrayMediumRegex with global flag alternates true/falseHardIterator is single-use — second loop produces nothingMediumCustom error class not extending Error

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab