JavaScript Quirks

Type Coercion: Sum as Number

easyjs-quirkscoercionnumber
0:00

Problem

The expression `"5" + 3` produces `"53"` because `+` coerces numbers to strings when one operand is a string. Implement `addAsNumbers(a, b)` that always returns the numeric sum, even when inputs are numeric strings.

Your solution
Test results
Click Run tests to check your solution against the test cases.

About this challenge

Type Coercion: Sum as Number is a easy javascript quirks coding challenge. It explores a surprising corner of JavaScript's behavior — the kind of gotcha that causes real bugs. Write your solution in the editor above, run it against the test cases, and get instant feedback — free, in your browser, no sign-up.

Show a hint & approach (spoiler)

Hint: Coerce both inputs to Number() before adding.

Approach: The unary + or Number() forces numeric conversion. Without it, string + number returns a string due to JS overloading + for concatenation.

Keep practicing

Browse all coding challenges, or build intuition for the underlying concepts with the algorithm visualizers and the developer cheatsheets.