Type conversions caused by JavaScript double equals sign

Source: Internet
Author: User

Implicit type conversion steps

First, see whether there is a nan before or after the double equals sign, and return False if there is a Nan.

Second, see whether there is a Boolean before and after the double equals sign, Boolean converts the Boolean to a number. (False is 0,true is 1)

Third, then look at the double equals sign before and after there are no strings, there are three kinds of situations:

1, the other side is the object, the object uses ToString to convert;

2, the other side is the number, the string to the number; (previous examples already)

3, the other side is a string, direct comparison;

4, other return false

Four, if it is a number, the other side is the object, the object takes valueof to compare, the other all return false

Five, null, undefined do not type conversions, but they are both equal

For example to consolidate and improve

Let's do the following topics together!

varA;console.dir (0 = =false);//trueConsole.dir (1 = =true);//trueConsole.dir (2 = = {valueOf:function(){return2}});//trueConsole.dir (A= = NaN);//falseConsole.dir (nan = = Nan);//falseConsole.dir (8 = = undefined);//falseConsole.dir (1 = = undefined);//falseConsole.dir (2 = = {toString:function(){return2}});//trueConsole.dir (undefined==NULL);//trueConsole.dir (NULL= = 1);//falseConsole.dir ({toString:function(){return1}, ValueOf:function(){return[]}} = = 1);//trueConsole.dir (1== "1");//trueConsole.dir (1=== "1");//false

Type conversions caused by JavaScript double equals sign

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.