A Brief Introduction to the implicit type conversion-JavaScript technique for javascript Data Types

Source: Internet
Author: User
This article briefly introduces information about implicit type conversion of JavaScript data types. For more information, see the following JavaScript data types: null, undefined, boolean, string, number, object. Object is a reference type. The other five types are basic or original types. We can use the typeof method to print a type. To compare variables of different types, you must first convert them to type, which is called type conversion and type conversion is also called implicit conversion. Implicit conversion usually occurs in the addition, subtraction, multiplication, division, equals, and smaller than, greater than, and so on ..

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

This topic describes implicit data type conversion in javascript, which is easy to use and can simplify many operations.

See the following code example:

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

The code above is an operation that implicitly converts an array into a string. Is it much easier than the following method:

var arr = [5];console.log(arr.toString());

Implicit data type conversion like above is widely used in actual encoding. The following is the question.

1. Data type conversion between value types:

For details about the data types in javascript, refer to the section about javascript data types.

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

If you use the + operator to perform operations on numbers and strings, the numbers are first converted to strings and then connected to strings:

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

(2). + operator operations involving boolean values:

If the boolean type is involved, the Boolean value is first converted to the corresponding number or string, and then the corresponding string connection or arithmetic operation is performed.

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

The above code first converts true to number 1 and then performs arithmetic addition.

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

The Boolean value above will be converted to the corresponding string form "true", and then connected to the string.

(3). subtraction operation:

If the subtraction operation is performed, the two operands are first converted to numbers, and then the arithmetic operation is performed:

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

True is converted to number 1, string "8" is converted to number 8, and then performs arithmetic operations.

Multiplication, division, greater than, less than and minus conversion is the same, no example is given.

(4). = equality calculation:

Undefined and null are special. They use the = Operator to return true.

console.log(undefined==null);

When other value types are compared, the Operation count is converted to a number.

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

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

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

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

Ii. reference type to value type:

Converting a reference type (object) to a value type is much more complicated. The following describes the distribution.

The two methods inherited by objects can help us to convert objects to value types:

(1). toString () method.

(2). valueOf () method.

Generally, we think that to convert an object to a string, we need to call the toString () method, and to convert it to a number, we need to call the valueOf () method, but it is not that simple in actual application, see the following code example:

Var obj = {webName: "", url: "softwhy.com"} console. log (obj. toString ());

The code above shows that the toString () method does not convert an object into a string that can reflect this object.

var arr = [1, 2, 3];console.log(arr.valueOf());

The code above shows that the valueOf () method does not convert an object into a number that can reflect this object.

var arr = [1, 2, 3];console.log(arr.toString());

The toString () method of the array object can convert the array into a string that can reflect the array object.

Summary:

(1). Some objects simply inherit the toString () or valueOf () method, for example, the first example.
(2). Some objects do not inherit two methods, but are also overwritten.

Therefore, some object methods can achieve the goal of converting to strings or numbers, while others cannot.

The following rules call toString () or valueOf () to convert an object to a string or number:

When toString () is called, if the object has this method, this method is called. If this method returns a value type data, this value type data is returned, then convert the data type according to the context. If no toString () exists, or the return value of this method is not a value type data, valueOf () will be called (if this method exists), if valueOf () if a value type data is returned, the corresponding data type conversion is performed based on the context.

Further description:

(1 ). the previous section describes the functions of the valueOf () and toString () methods by default (convert an object to a number or string). However, it is not a hard rule, that is to say, the valueOf () method must not return a number or the toString () method must be converted to a string, for example, the two simple inheritance methods cannot be used to convert to numbers and strings. For example, we can acknowledge the two methods by ourselves, and the return value does not need to be a number or string.

(2 ). note that many friends think that the toString () method must be called before conversion to a string. In fact, this is a wrong idea. We should understand this and call toString () the toString () method is called first.

See 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 obviously the valueOf () method called, rather than the toString () method. Some may have this question: isn't the toString () method called if a number like [2] is converted to a string "2.

The Code is as follows:

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

In fact, the process is like this. First, arr will call the valueOf () method, but the number method is simply inherited and not overwritten (of course, This rewriting is not implemented ), the returned value is an array object and is not a value type. Therefore, the toString () method is called to convert the returned value to a string.

Summary:

Most objects that are implicitly converted to the value type are first attempted to call the valueOf () method. However, the Date object is an exception. The valueOf () and toString () methods of this object have been carefully rewritten. By default, the toString () method is called. For example, the + operator is used, in other arithmetic environments, the valueOf () method is called instead.

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 content is all about implicit type conversion of JavaScript data types. I hope you will like it.

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.