I found thoose examples in another thread but I dont get it.
0 == \'0\' // true
0 to the left i converted to false(the only num
When you use ==
JavaScript will try very hard to convert the two things you are trying to compare to the same type. In this case 0
is converted to '0'
to do the comparison, which then results in true
.
You can use ===
, which will not do any type coercion and is best practice, to get the desired result.
The equality operator converts the operands if they are not of the same type, then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
JavaScirpt Table Equality: http://dorey.github.io/JavaScript-Equality-Table/