A brief introduction to the implicit type conversion of JavaScript data types _javascript Tips

Source: Internet
Author: User
Tags arithmetic inheritance

The data type of JavaScript is divided into six types, null,undefined,boolean,string,number,object respectively. object is a reference type, and the other five are of the base type or the original type. We can use the TypeOf method to print out what kind of it belongs to. Variable comparisons of different types have to be typed first, called Type conversions, and type conversions are also called implicit conversions. Implicit conversions usually occur in operator subtraction, equal to, and less than, greater than, etc...

typeof '//string    
typeof (one)//number ' one '
< 4   //false

This chapter describes the implicit data type conversions in JavaScript, and for its good mastery, it can simplify many operations in practical applications.

Look at the following code example:

var arr = [5];
Console.log (arr+ "");

The code above is an operation that implicitly converts an array to a string, which is much simpler than the following:

var arr = [5];
Console.log (Arr.tostring ());

Implicit data type conversions like the above apply a lot to the actual coding, so let's get to the point.

A. Data type conversions between value types:

Data types in JavaScript can be found in the JavaScript data type details section.

(1). Use the + operator for numbers and strings:

Numbers and strings if you operate with the + operator, the number is converted to a string, and then the string concatenation operation is performed:

var antzone = "Antzone";
var num = 8;
Console.log (Antzone+num);

(2). The + operator operation with Boolean value participation:

If a Boolean participates, the Boolean value is first converted to the corresponding number or string, and then the corresponding string concatenation or arithmetic operation is performed.

var bool = true;
var num = 8;
Console.log (bool + num);

The code above is to convert true to number 1 before arithmetic plus.

var bool = true;
var num = "8";
Console.log (bool + num);

The above Boolean value is converted to the corresponding string form "true" and then the string is concatenated.

(3). Subtraction Operation:

If you subtract, then the two operands are converted to numbers first and then the arithmetic:

var bool = true;
var num = "8";
Console.log (Bool-num);

True is converted to the number 1, and the string "8" is converted to the number 8, and then the arithmetic operation is performed.

multiplication, in addition to, greater than, less than and minus the conversion is the same, no longer cite examples.

(4). = = equal-Sex operation:

Undefined and null are special, and they both use the = = operator to return the value is true.

Console.log (Undefined==null);

When other value types are compared, the operands are converted to numbers

Console.log ("3" ==3);

The code above converts the string "3" to a number and then compares it.

Console.log ("1" ==true);

The above code converts "1" and true to numbers, and then compares them.

Two. Reference type transfer value type:

It is much more complicated to convert a reference type (object) to a value type, as described in the following distribution.

The two methods of object inheritance can help us implement the conversion function of object to value type:

(1). ToString () method.

(2). valueof () method.

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: "Cloud-dwelling community",
 URL: "softwhy.com"
}
Console.log (obj.tostring ());

As you can see from the above code, the ToString () method does not convert an object to a string that reflects the object.

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

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

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

The ToString () method of an Array object can convert an array to a string that can reflect this array object.

Summarized as follows:

(1). Some objects simply inherit the ToString () or valueof () method, such as the first example.
(2). Some objects not only inherit two methods, but also rewrite them.

So some of the object's methods can be achieved by converting to a string or a number of targets, some not.

The rules for calling ToString () or valueof () to convert an object to a string or a number are as follows:

When you call ToString (), this method is called if the object has this method, and if this method returns a value type data, the value type data is returned, and then the related data type conversion is made based on the context environment in which it is in. If there is no ToString (), or if the method returns a value that is not a value type data, then the valueof () is called (if this method exists), and if valueof () returns a value type data, then the associated data type conversion is based on the context environment in which it is found.

Further explanation:

(1). The valueof () and ToString () methods are typically used by default (converting objects to numbers or strings), but it is important to note that this is not a hard rule, which means that not the valueof () method must return a number or ToString () method must be converted to a string, such as the simple inheritance of these two methods can not be implemented to convert to numbers and strings, for example, we can give thanks to these two methods, the return value is not necessarily a number or string.

(2). It is also important to note that many friends believe that the conversion to a 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 called the ToString () method first.

Look at the following code example:

var arr = [];
arr.valueof = function () {return "1";}
arr.tostring = function () {return "2";}
Console.log (arr + "1");

In the above code, ARR is to be converted to a string, but it is obvious that the valueof () method is invoked without invoking the ToString () method. Some friends may have such a question, is [2] such a number converted to the string "2", is not called the ToString () method.

The code is as follows:

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

In fact, the process is this, first arr will first call the valueof () method, but the number of this method is simply inherited, 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.

Summarized as follows:

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.

The code example is as follows:

var date = new Date ();
Console.log (date + "1");
Console.log (date + 1);
Console.log (date-1);
Console.log (date * 1);

The above is a small series to introduce the JavaScript data types of the implicit type conversion of all the content, I hope you like.

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.