问题
I've recently noticed something strange when I used if( ... == true) in JavaScript. The == operator should attempt to convert the given data-types to be the same. But some cases don't appear to do that as we would expect:
if ( 42 == true ) // false ( Only 1 is true )
if ( "Hello World" == true ) // false ( false for any string )
Although if I convert them myself doing !! to both 42 and "someString" turn out to be true:
if ( !!42 === true ) // Shows that 42 cast to a boolean is true.
if ( !!"Hello World" === true ) // Same for strings
Note if I switch this around if( true == ... ) the results are the same. I'm wondering exactly what is going on here, why the == is converting these items oddly when they indeed convert into true. How is JavaScript trying to convert these items? Is it trying to convert the booleans to a number or string instead?
My main question would be exactly how does the JavaScript equals operator ( == ) work when attempting to convert different data-types to be the same in an expression, when a boolean is in the expression in this case?
Edit: I want to note that while I did use !! in my question, it was simply to show that if I convert the given datatypes into Booleans that they do indeed turn out to by true, while when == attempts to convert the result is different. This question is in no way asking about what !! is.
回答1:
As Crockford says: "The rules by which they do that are complicated and unmemorable." The spec defines them all in section 11.9.3 (pointed out by @Oriol in a comment to OP).
For the two cases you provided:
if ( 42 == true ) // false ( Only 1 is true )
if ( "Hello World" == true ) // false ( false for any string )
In case 1, y is a Boolean, so it gets converted to a number (step 7). The number conversion of true is 1. So now we're evaluating 42 == 1. This is obviously false.
In case 2, y is again a Boolean, but this time, x is a string. Per step 7, y is converted to a number, so the comparison is now "Hello World" == 1. Per step 5, x is now converted to a number. The numerical representation of an arbitrary string is NaN. Now NaN == 1 is being compared. As it says in step 1ai, that's false.
Again, as Crockford said...
回答2:
It appears to me that == means "evaluates to the same value". 42 does not equal true because true is being converted to an integer for the comparison, and 42 != 1. But if people want to convert 42 to a Boolean, simply do if (42), or you can do the !! not not trick. Most people just do if (42), converting the non-zero, non-empty value to a bool with the parenthetical.
(x == true) should only be used when x is expected to be either 0 or 1, or true or false.
And I gather you know the difference between == and === already.
(1 == true) // true
(1 === true) // false, because data types aren't converted when using ===
For more info on automatic type conversions and assignment shortcuts, I recommend reading this page. While it doesn't specifically answer your question, it does provide examples illustrating when you should be specific for what you are testing for.
来源:https://stackoverflow.com/questions/26949187/how-exactly-does-javascipts-data-type-convertion-work-for-operator