MediumType Coercion🐛 Debug Challenge

Plus operator concatenates strings instead of adding

Buggy Code — Can you spot the issue?

function getTotal(a, b) {
  console.log(a + b);
}

getTotal('10', '20');

Fixed Code

function getTotal(a, b) {
  console.log(Number(a) + Number(b));
}

getTotal('10', '20');

Bug Explained

Bug: When either operand of + is a string, JS converts the other to a string and concatenates. "10" + "20" = "1020".

Explanation: Number() converts strings to numbers first. Then + performs numeric addition.

Key Insight: + prefers string concatenation when either operand is a string. Always explicitly convert with Number() when you need addition.

More Type Coercion Debug Challenges

EasyNaN comparison is always falseEasyLoose equality treats 0 and "" as false

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab