Explanation of the implicit type conversion in JavaScript caused by double equal sign _ Basics

Source: Internet
Author: User
Tags numeric value object object

Introduction

If statement should be more than a programmer's statement, many times to do if judgment, if statements generally use double equals to determine whether the two elements are consistent, if it is consistent, then return is true, and then execute the following statement, otherwise, the execution of other statements. The implicit type of conversion mentioned in this article is the conversion caused by = =. For a simple example, the double equals sign is not the full equals sign, the full equals "= = =" Three equals sign, the statement "1" ==1, then the previous string "1" is converted to the number 1 and then compared. In this example, you should know what the conversion of the recessive type is!

Implicit type conversion steps

First, look at the double equals before and after there is no Nan, if there is Nan, all return false.

Second, look at the double equals before and after the Boolean, Boolean will convert Boolean to a number. (False is 0,true is 1)

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

1, the other side is the object, the object uses ToString () or valueof () for conversion;
2, the other is a number, a string of digits; (previous examples)
3, the other is a string, direct comparison;
4, other return false
If it is a number, the other side is the object, the object takes valueof () or ToString () for comparison, all others return false

Five, null, undefined do not convert to type, but they are equal

The conversion sequence above must be kept in mind that when interviewing, there are often types of problems.

. ToString () method and. valueof () method numeric conversion

In general, we think that converting an object to a string calls the ToString () method, which converts to a number to invoke the valueof () method, but it is not as simple as the actual application, looking at the following code example:

var obj = {
 webName: "Haorooms Front-End Blog",
 URL: "www.jb51.net"
}
Console.log (obj.tostring ());//[object Object]

Likewise, let's look at the valueof () method:

var arr = [1, 2, 3];
Console.log (Arr.valueof ());//[1, 2, 3]

As you can see from the code above, the valueof () method does not convert an object to a number that can reflect this object. Instead, we use ToString ()

var arr = [1, 2, 3];
Console.log (Arr.tostring ());//1,2,3

Note: Many friends believe that the conversion to the string first calls the ToString () method, in fact this is the wrong understanding, we should understand that, call the ToString () method can be converted to a string, but not necessarily the conversion string is the first call ToString () method.

Let's look at the following code:

var arr = {};
arr.valueof = function () {return 1;}
arr.tostring = function () {return 2;}
Console.log (arr = = 1);//true

var arr = {};
arr.valueof = function () {return [];}
arr.tostring = function () {return 1;}
Console.log (arr = 1);//true

The above code we can see, the conversion first call is valueof (), if valueof () is not a numeric value, then call tostring for conversion!

var arr = {};
arr.valueof = function () {return "1";}
arr.tostring = function () {return "2";}
Console.log (arr = = "1");//true

If "1" is a string, then it first calls the valueof ().

var arr = [2];
Console.log (arr + "1");//21

The example above, which is called toString (), is 2 after arr.tostring ().

The conversion process is like this, first arr will first call the valueof () method, but the number of this method is simple inheritance, and did not rewrite (of course, this rewrite is not our implementation), the return value is the array object itself, not a value type, so instead call the ToString () method, The purpose of converting to a string is achieved.

Summary

Most objects implicitly convert to value types are the first attempt to invoke the ValueOf () method. However, the Date object is an exception, and the valueof () and ToString () methods of this object are all carefully rewritten, by default calling the ToString () method, such as using the + operator, which in turn calls the ValueOf () method in other arithmetic environments.

var date = new Date ();
Console.log (date + "1"); Sun Apr 2014 17:54:48 gmt+0800 (CST) 1
console.log (date + 1)//sun Apr 2014 17:54:48 gmt+0800 (CST) 1
Consol E.log (date-1);//1460886888556
console.log (date * 1);//1460886888557

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.