I used to use it every time
= =When the type conversion is always confused, do not know how to judge, so do a simple summary, later only apply some rules can be accurately judged. Congratulations, congratulations.


Type conversion for if

Use Boolean () to cast if (); use Boolean ();

  • String: true for all but empty strings;
  • Number: true except +0, -0, NaN
  • Boolean: Do not convert
  • Undefined: false
  • Null: false
  • Object: true,

For example:

if ([]) {
    console.log (1)//1
}


if (' 'Console. log (1)// No output}Copy the code

Comparison of the same type with the same value

Moving on to a simple comparison that doesn’t involve casting, look directly at the code:

null == null//true

undefined == undefined//true

NaN == NaN//falseThe special case' '= =' ' //true0 = = 0 / /true

true= =true//true{} = = {} / /falseBecause the references of the two objects are not equal [] == [] //false, because references to two arrays (objects) are not equal var a =function () {} 
var b = function() {}
a == b//falseVar c = d =function() {} 
c == d //true, because a and B store the same references to the functionCopy the code

Conclusion: The comparison between values of the same type and the same type using == is equivalent to the comparison between values of the same type, and the comparison between different values of the same type does not occur type conversion.



Different types of comparison

The comparison between the different types is complicated and makes no sense, and implicit type conversions are a big hole.

The Number conversion rule is as follows:

Undefined, NaN, and null are converted to NaN, NaN, and 0, respectively

Boolean: true –> 1, false –> 0

String: “ABC” — – > NaN, “123” — – > 123, ‘ ‘- – > 0, “‘ – > 0, ‘\ n’ – > 0

Let’s start with the easy ones:

null == undefined //true
null == NaN //false
undefined == NaN //false
NaN == NaN //falseCopy the code

These are really special, just keep in mind, especially NaN, which doesn’t compare to itself, let alone other types of values.

Take a look at

null == 0; //false
null == '0'//false
null == ' '//false
null == ' '//false
null == false//false
null == {}//false
null == []//false
Copy the code

Null is false except when compared to null or undefined. Similarly, undefined yields false when compared to undefined or nul, if you are interested.

Moving on, without looking at the comparison of reference types:

0 = ='0'//ture

0 == ' '//trueThe Number (' ') returns 0 0 ==' '//trueThe Number (' ') returns 0 0 ==false//trueThe Number (false) returns 0 1 ==true//trueThe Number (true) returns 1 / / -- -- -- -- -- - the following is the comparison of reference types, look back -- -- -- -- -- -- -- - [] the toString () / /' '0 = = [] / /trueValueOf is called to return the valueOf the reference type, and toString is called to return //' 'The Number (' ') returns 0 [123]. The tostring () / / "123", Number (" 123 ") back to 123, 123 = = [123] / /trueValueOf is called to return the valueOf the reference type. ToString is called to return"123"/ / return"123"Type conversion occurs again, Number("123"Var obj = {valueOf:function() {return 1
	}
}

1 == obj//trueVar obj2 = {valueOf:function() {return '1';
	}
}

1 == obj2//trueVar spam = {valueOf:function () {
        return {
            valueOf: function () {
                return1}}}}; 1 == spam//false
spam == "[object Object]"//true, obviously calling the toString method.Copy the code

Except for reference types, non-numeric types are converted by the Number function and compared to numbers. Reference types are discussed later.

Moving on, forget about the comparison of reference types

' '= =false //trueThe Number (' ') returns 0, Number(false) returns 0' '= =false // trueThe Number (' ') returns 0, Number(false) returns 0' '= = 0 / /true

'1'= =true //true
'0'= =false//true
"0.00"= =false//true

'js'= =true//false
'js'= =false //falseThe Number ('js') returns NaN / / -- -- -- -- -- - the following is the comparison of reference types, look back -- -- -- -- -- -- -- -- the var egg = {}; egg.toString()//"[object Object]"
"[object Object]" == egg //trueToString var obj = {valueOf:function() {return1}}'1' == obj//trueValueOf returns 1 and puts the left'1'Type conversion.Copy the code

A little irrational, but not without rules:

Except for reference types, values compared to strings appear to be converted using the Number function, and the string itself is converted using the Number function.

If you look back at the previous comparisons and ignore the reference type, it’s easy to see that valueOf is called. If valueOf returns a valueOf a reference type, the toString method is called.

Verify with Boolean values:

obj == true//true, obj calls the valueOf method and returns 1trueGo to number, obj ==false//false

obj2 == true//trueObj returns after calling the valueOf method'1', and then obj andtrueGo to number obj2 ==false//false

var egg = {};
egg == false//falseThe egg returns after calling the toString method"[object Object]"NaN egg ==true//false
Copy the code

conclusion

In the case of == :

  1. Both un______ and NULL are equal only to undefined or null.
  2. NaN is not equal to any of these values

When type conversions occur with ==, except for undefined, null, and NaN:

  1. Values of the base type compared with number are converted to number using the number function for comparison.
  2. A valueOf or toString method is called for a valueOf a reference type compared to number. If it does not return a number, the value is converted to number again for comparison.
  3. The value of the base type to be compared with a string. Both the string and the base type are converted to the Number type using the Number function for comparison.
  4. A valueOf a reference type compared to a string is first called valueOf or toString. If it does not return a number, it is typed to number again and then compared.
  5. ValueOf or toString methods are first called for values of reference types compared to Booleans. If a non-Boolean value is returned, rules 1, 2, and 3 are used to determine the value.
  6. If the toString method returns a reference value, valueOf is attempted, and vice versa.

To refine it:

When type conversions occur with ==, except for undefined, null, and NaN:

  1. For comparison between basic types, enter number
  2. If there is a reference type, check whether valueOf returns a valueOf a primitive type. If so, call valueOf. If not, call toString. For primitive types, return rule 1. If you are interested, try overwriting both valueOf and toString and then returning the valueOf the reference type, you will get a merciless error.


The last

The conclusion of this paper is purely inductive reasoning, there are certainly not rigorous, I hope you don’t hesitate to comment.


reference

Object.prototype.valueOf()