Implicit conversion in js

Source: Internet
Author: User
Comparison conversion rules between different data types in js for implicit conversion are as follows:

1. Comparison between objects and boolean values

When an object is compared with a Boolean value, the object is first converted to a string and then to a number. The Boolean value is directly converted to a number.

[] = True; // false [] convert to string '', then convert to number 0, true to number 0, so false

2. Comparison between objects and strings

When the object is compared with a string, the object is converted to a string, and then the two are compared.

[, 3] = '1, '// true [, 3] is converted to '1, 2, 3', then and '1, 2, 3 ', so returns true;

3. Comparison of objects and numbers

When comparing an object with a number, the object is first converted to a string, then converted to a number, and then compared with a number.

[1] = 1; // true: the object is first converted to a string and then to a number, and then compared with [1] => '1' => 1, so the result is true.

4. Comparison of strings and numbers

When comparing a string and a number, convert the string to a number and then compare the two.

'1' == 1 // true

5. Comparison of string and boolean values

When the string and boolean values are compared, both are converted to numerical values and then compared.

'1' == true; // true

6. Comparison of Boolean values and numbers

When a Boolean value is compared with a number, the Boolean value is converted to a number and the two are compared.

true == 1 // true

Many children's shoes that just came into contact with js have seen so many conversion rules. In fact, the rule is very simple. You can write down this picture (it's time to show my superb painting skills)

In addition, let's look at some things that require "special care.

Let's look at an interesting question.

[] == false;![] == false;

The two results are true. The first one is: object => string => value 1 false to convert to Number 1. This is true. It should be OK,
The second front side has more !, Convert it to a Boolean value and return the inverse value. If it is converted to a Boolean value, all null strings (''), NaN, 0, null, and undefined return true! [] This [] => true returns false, so [] = false is true.

There are also some things to remember, such:

Undefined = null // true returns true when comparing undefined and null, and returns falseNumber (null) when comparing them with other values // 0

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.