primitive value
- undefined
- null
- boolean
- number (only one number type…)
- string
- symbol (ES6)
coercion
1 2 3 4 5 6 7 |
var a = 1 + '2'; console.log(a); // 12 /* JavaScript engine coerce 1 into string (dynamic typing) */ console.log(3 < 2 < 1); // true /* by operator precedence it will became false < 1 and coerce false to 0*/ |
expected
1 2 |
true == 1 //true |
weird parts
1 2 3 4 5 6 7 8 |
null == 0 //false null < 1 //true "" == 0 //true "" == false //true |
loose equal vs strict equal
use === to prevent coercion and do not use == in 99% cases except you mean to coerce
ref:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness