Type Coercion in Arithmetic
Predict Output Medium
Question
What does this code print?
Code
console.log("10" - "3");
Hint
The - operator only works on numbers, so strings are coerced.
Explanation
The - operator in JavaScript only works on numbers, so both "10" and "3" are coerced to numbers 10 and 3. The result is 10 - 3 = 7 (a number, not a string). This contrasts with + which does string concatenation when either operand is a string.
Related Resources
Related Lessons
- Data Types
Learn about primitive data types.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.