-1 == true; // false -1 == false // false -1 ? true : false; // true
Can anyone explain the above output? I know I could work round this by comparing to 0 but I'm interested. I'd expect at least one of the sloppy equals statements to be true as they do implicit type conversion, and I certainly didn't expect the ternary to come up with a totally different result.
All non-zero values will be converted to true , and zero values to false . With negative numbers being non-zero, they are converted to true .
To use negative numbers, just place a minus (-) character before the number we want to turn into a negative value: let temperature = -42; What we've seen in this section makes up the bulk of how we will actually use numbers.
Fuzzy line indeed! Use the Boolean(-1) for a true cast the way you expect - and discover -1 is still considered true (as in not 0). These truthy/falsy values in JS (or any other dynamic language) values are just a hindrance more than they are useful; I always now fully compare in my conditionals.
In the first two cases, the boolean is cast to a number - 1 for true and 0 for false. In the final case, it is a number that is cast to a boolean and any number except for 0 and NaN will cast to true. So your test cases are really more like this:
-1 == 1; // false -1 == 0; // false true ? true : false; // true
The same would be true of any number that isn't 0 or 1.
For more detail, read the ECMAScript documentation. From the 3rd edition [PDF], section 11.9.3 The Abstract Equality Comparison Algorithm:
19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
It's worth giving the full algorithm a read because other types can cause worse gotchas.
In most systems, non-zero values are considered a true value, but that doesn't necessarily mean that they are the same true value as true
. Thus, -1 == true
doesn't necessarily hold, but -1
can still be considered a true value since it is non-zero.
Really, though, you shouldn't be comparing integers to booleans if you can avoid it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With