[] == ! [] for what is true

Source: Internet
Author: User
Tags first string

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.