the "= =" Symbol and the Boolean conversion rule in JS

Source: Internet
Author: User

What is the rules for how = = converts types?

Comparison rules for "= =":

1. Comparing numbers and strings would always convert the strings to numbers.

The number type is compared to the string type, and the string is converted to the number type. Example: 2== "2" true

2. Null and undefined would always equal the other.

The null type is always equal to the undefined type comparison. such as: var a = null,b; Then A==b is true.

3. Comparing Booleans to any other type would always cause the booleans to being converted to numbers.

A Boolean type is compared to any other type, and the Boolean type is converted to the number type. For example: var a = 0, B = false; A==b is true

4. Comparing numbers or strings to objects would always cause the numbers or strings to being converted to objects.

The number type or string type is compared to the object type, and number or string type is converted to type object. For example: var a = 0, b = {}; A==b is False

The rules for converting and types to Booleans is actually relatively straightforward:

about other types of rules that are converted to Boolean types:

1. Undefined and null is always false.

2. Booleans is just treated as booleans (obviously).

3. Numbers is false if they equal 0 or NaN; otherwise, they ' re true.

4. Strings is true, except for the empty string "", which is false.

5. Objects is always true.

the "= =" Symbol and the Boolean conversion rule in JS

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.