There is such a question in a test question set compiled by cssrain:
<SCRIPT LANGUAGE="JavaScript">
var a = 0;
var b = -1;
var c = 1;
function assert (aVar) {
if (aVar==true) alert(true);
else alert(false);
}
assert(a);
assert(b);
assert(c);
</SCRIPT>
Run code box
[Ctrl+A Select All Tips: You can modify part of the code first and then press Run]
According to my understanding, any boolean value that is not 0 should be true.
But the correct output of this question is: false false true.
The value of (-1==true) is false.
Let’s look at the following example:
<SCRIPT LANGUAGE="JavaScript">
var a = 0;
var b = -1;
var c = 1;
function assert (aVar) {
if (aVar) alert(true);
else alert(false);
}
assert(a);
assert(b);
assert(c);
</SCRIPT>
Run code box
[Ctrl+A Select All Tips: You can modify part of the code first and then press Run]
The running results are: false, true, true.
Here, we find that the results of if(aVar) and if(aVar == true) are not the same.
cssrain's explanation in the answer is:
if(aVar) and if (aVar==true) have very different answers for negative numbers.
Is it really the reason for the negative number? Consider the following example:
Run code box
[Ctrl+A Select All Tips: You can modify part of the code first and then press Run]
Why does the positive number 2 also return false? Let's convert the number into a boolean value and have a look.
Run code box
[Ctrl+A Select All Tips: You can modify part of the code first and then press Run]
The non-zero Boolean values here are indeed true, which means that all problems focus on the == operator in 2==true. It is basically certain that == must not convert numbers into Boolean values and then compare them.
Look what ECMA-262 (page 80) says:
6.If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
7.If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
That is, Boolean values are first converted to numbers and then compared. The numeric value of true is 1 and false is 0. So neither 2 nor -1 can be equal to true.
Take a closer look at the following example:
<SCRIPT LANGUAGE="JavaScript">
var a = "undefined";
var b = "false";
var c = "";
function assert (aVar) {
if (aVar==true) alert(true);
else alert(false);
}
assert(a);
assert(b);
assert(c);
</SCRIPT>
Run code box
[Ctrl+A Select All Tips: You can modify part of the code first and then press Run]
According to the previous idea, true will be converted to 1, so all three statements will return false. Run it and find that it is indeed the case.
Next, change if(aVar==true) to if(aVar).
Run code box
[Ctrl+A Select All Tips: You can modify part of the code first and then press Run]
The running results at this time are true, true, false. Because the results of Boolean("undefined"), Boolean("false"), and Boolean("") are true, true, and false. Non-empty strings are converted to Boolean true.
Finally, there is an example to explain the comparison rules when both sides of == are strings and numbers.
Run code box
[Ctrl+A Select All Tips: You can modify part of the code first and then press Run]
Did you find that this "001"==true is true.
Because true is converted to 1 first. Then refer to ECMA’s rules:
4.If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
5.If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
The string is to be converted to a number, and the value of Number("001") is also 1, so the result is true.