Introduction to converting objects into numbers or strings in JavaScript, objectstring

Source: Internet
Author: User

Introduction to converting objects into numbers or strings in JavaScript, objectstring

During JavaScript programming, JS automatically converts the object to number or string based on different contexts before processing. The automatic conversion rules are as follows:

Rules for automatically converting an object to a string:

1. If the class of the object overwrites the toString () method, this method is called. If the return result of toString () is Primitive (string, number, boolean, undefined, null), the value of Primitive is converted to string and returned.

2. if the class to which the object belongs does not overwrite the toString () method-toString (), the returned result is "[object Object]"; or overwrite toString () the method returns an object. JS will call the valueOf () method of the object. If the returned result of the valueOf () call is Primitive (string, number, boolean, undefined, null ), convert the Primitive value to a string and return the result.

3. If neither of the above two conditions is met, and the Primitive value cannot be obtained by calling the toString () method or valueOf () method of the object, JS will throw a TypeError.

Rules for automatically converting an object to a number:

1. Call the valueOf () method of the object. If the value of Primitive is obtained, convert the value of Primitive to number and return the result.

2. if the value of Primitive cannot be obtained from the valueOf () method, the toString () method of the object is called. If toString () returns the value of Primitive, convert the Primitive value to number and return the result.

3. If neither of the above two conditions is met, JS will throw a TypeError.

As you can see, the rules for automatically converting an object to a string and an object to a number are actually the same. The difference lies in the call sequence of the toString () method and the valueOf () method.

According to the above rules, some conversion results can be well understood:


1. If an empty array is converted to a number, the result is 0. This is because the valueOf () method of array will be called first, Because valueOf () returns the array object itself, then JS will call the toString () method of the empty array; because the returned result of an empty array toString () is a null string, the Null String is converted to a number 0 and then returned.

2. For an array with only one number Member, apply the same rule to convert it to number. The final result is the number.

3. For arrays with multiple numeric members, the final result is NaN because the string cannot be converted to number.

When to convert to string? When to convert to number?

During Automatic object type conversion, JS will choose to convert the object type to string or number based on the object type and the operator. The specific rules are as follows:

1. if an object appears on both sides of the + operator, convert the object to a string.
2. All objects (except Date objects) are preferentially converted to number.
3. For Date objects, convert them to string first.

It is worth noting that for the + operator, except for the objects or strings on both sides of the operator, the "convert to number" operation is executed in other cases. At the same time, this operation is also related to the order between values.

Lab


Copy codeThe Code is as follows:
Console. log (3 * []); // 0
Console. log (3 * [2]); // 6
Console. log (3 * [1, 2]); // NaN

Console. log (3 + [2]); // 32

Var now = new Date ();
Console. log (now + 1); // Wed Mar 26 2014 10:51:39 GMT + 0800 (CST) 1
Console. log (now-1); // 1395802299223
Console. log (now * 2); // 2791604598448

Console. log (true + true); // 2
Console. log (2 + null); // 2, null is converted to 0
Console. log (2 + undefined); // NaN, undefined is converted to NaN
Console. log (1 + 2 + "cats"); // 3 cats
Console. log (1 + (2 + "cats"); // 12 cats

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.