Let's consider this question first, what will Console.log ([] = = false) print?
The answer is true. Why is it?
First, because when one of the "= =" Numbers is a Boolean value, it is first converted to a number (ECMAScript specification). So it becomes a beg [] = = 0.
Then the question is why []==0 will be true? This is because when one side of the "= =" is a string or a number, and the other side is an object (an array is also an object), the object value is converted to the original value before it is judged equal. How is the value of the object converted to the original value? For all non-date mine objects, the conversion of the object to the original value is basically the conversion of the object to the number. There are 3 steps:
1. All objects call the ValueOf () method first, and if this method returns the original value, the object is converted to the original value.
2. If the ValueOf method returns a value other than the original, the ToString method is called, and if the ToString method returns the original value, the object is converted to the original value.
3. If neither the valueof nor the ToString method returns the original value, the TypeError exception is thrown.
OK, let's see [] What about the conversion to the original value? The [].valueof () method is called first, and the return value is the object itself, that is, [], which is not an original value. So the [].tostring () method continues to be called, returning an empty string "", which is an original value, so this value is converted to the output of the original value as an object. So the question becomes "= = 0."
Finally, why is "= = 0" true? I believe many students know, when "= =" on either side of a string is a number, the first string into a number, and then compare. "" turns into a number 0, so the last [] = = False is True.
Let's go back to the title [] = =! [] for what is true.! The priority is higher than = =, so it will be executed first! []。 The first is to turn the [] into a Boolean type and then take the inverse. [] Turn Boolean value is true, why? Because there are only 5 false values in JavaScript except for false itself, "", Undefined, null, 0, NaN, respectively. All other value-to-Boolean types are true except for these 5 false values. All objects are truth, including new Boolean (false). So the question is what we discussed just now [] = = False. So get [] = =! [] is true.
[] == ! [] for what is true