Java Reference
In-Depth Information
As you can see, JavaScript is returning true when we are checking if the variable an-
swer is equal to the string "5", when in fact answer is equal to the number 5. This is an
important difference, but when a soft inequality is used, JavaScript will attempt to coerce
the two values to the same type when doing the comparison. This can lead to some very
strange results:
" " == 0;
<< true
" " == "0";
<< false
false == "0";
<< true
"1" == true;
<< true
"2" == true;
<< false
"true" == true;
<< false
null == undefined;
<< true
As you can see, values that are not actually equal have a tendency to be reported as being
equal to each other when using the soft equality operator.
Hard Equality
Hard equality also tests that the two values are the same type:
answer === 5;
<< true
answer === "5";
Search WWH ::




Custom Search