- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Type Coercion
JavaScript Type System
JavaScript Type Coercion
Type coercion is JavaScript converting types automatically, whether you wanted it or not.
It is the reason "5" + 3 and "5" - 3 give completely different answers.
Understanding it removes most of JavaScript's reputation for strangeness.
Example
Example
javascript
console.log("5" + 3);
console.log("5" - 3);The output is 53 then 2.
Plus joined them; minus converted them.
Why It Happens
Most operators expect numbers, so they convert what they are given.
+ is the exception: if either side is a string, it joins instead.
