Comparison rules for different types of javascript objects

Source: Internet
Author: User
Type (x) Type (y) Result
Type (x) = type (y) X = y
Otherwise... False
Null Undefined True
Undefined Null True
Number String X = toNumber (y)
String Number ToNumber (x) = y
Boolean (Any) ToNumber (x) = y
(Any) Boolean X = toNumber (y)
String or Number Object X = toPrimitive (y)
Object String or Number ToPrimitive (x) = y

 

Example 1:

[0] = true;

First, the toNumber of ture will be implemented, and the result is 1. The formula is as follows and the conversion is [0] = 1.

Then [0] will be toString (), the result is "0", and the formula is converted to "0" = 1

Then "0" will be toNumber (), the result is 0, and the formula is converted to 0 = 1

Finally, according to type (x) = type (y), the formula is converted to 0 = 1

Therefore, if [0] = true, false is returned.

 

Example 2:

"Potato" = true;

First, "true" is converted to "potato" = 1, and the result is 1.

Then "potato" will be toNumber and the result will be NaN. The formula is converted to NaN = 1.

According to typeof (x) = typeof (y), the formula is converted to NaN = 1

So "potato" = true will get false

 

Example 3:

Object with getValue

Str = new Number (1 );

Str. toString = function () {return "2 "};

Str = 1;

 

Here, typeof str = "object", so toPrimitive is applied to str. Here, valueOf is used to obtain 1.

So the formula is converted to 1 = 1

So str = 1 to get true

 

Example 4:

Object with toString

Var obj = {

ToString: function () {return "2 "}

};

Obj = 1

 

Here typeof obj = "object", toPrimitive for obj, first valueOf to get the object, continue toString () to get "2", formula for "2" = 1

Perform the toNumber operation on "2" to get 2, and convert the formula to 2 = 1.

So obj = 1 to get false.

 

 

Example 5:

[] = []

The left and right are different objects, so false

! [] = []

First! [] = False, here [] is an existing object. If you force it to be of the bool type, pass !! [] To get true, so! [] = False. The formula is set to false = []

Then false is set to 0, and the formula is set to 0 = [].

Then [] for "", the formula is for "0 =""

Then, convert "" to 0, and the formula is set to 0 = 0.

So! [] = [] To get true

Example 6:

! {}= {}

First! {} Is designed for "false". The rule is the same as "5", and the formula is converted to "false == {}

False is set to 0, and the formula is converted to 0 == {}

Perform toPrimitive on {} to get "[object Object]". The formula is designed for 0 = "[object Object]".

Perform toNumber on "[object Object]" to get NaN. The formula is set to 0 = NaN.

So! {}={} Returns false.

Related Article

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.