Introduction to value type conversion in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the value type conversion in JavaScript. This article describes the value type conversion rules, value type conversion and comparison, explicit type conversion, and the use of automatic type conversion, for more information, see +,-, *,/, =, And! In JavaScript ,! = During equal operation, if the value types on both sides of the operator are inconsistent with the expected type, JavaScript will convert the values on both sides of the operator to the expected type before performing the operation. When the expected value type is string, JavaScript converts the value to string. When the expected value type is number, JavaScript converts the value to number (if it cannot be converted to a value, NaN is returned) for example:

The Code is as follows:


Console. log (10 + "cats"); // 10 cats
Console. log (10 * "cats"); // NaN, "cats" will be converted to NaN
Console. log (10 + "2"); // 102
Console. log (10-"2"); // 8
Console. log (10/"2"); // 5
Console. log (10 * "2"); // 20
Console. log (10 * "2"); // 20
Console. log ("10" * "2"); // 20

Value type conversion rules

For The value conversion rules in JavaScript, refer to Table 3-2. JavaScript type conversions in "JavaScript-The Definitive Guide. Note the following:

1. After undefined is converted to number, the result is NaN.
2. Convert null to number and the result is 0.
3. Convert the empty string "" to number and the result is 0.
4. Convert-0 to string and the result is "0 ″.
5. After the empty array [] is converted to number, the result is 0.
6. An array with only one number member (for example, [9]) is converted into a number and the result is the numer value (9 ).

When JavaScript converts a string to a number, there are two interesting rules:

1. JavaScript will delete the blank characters at the beginning and end of the string before conversion. Therefore, strings like "42" can be converted to numbers 42 smoothly.

2. After the blank characters at the beginning and end are deleted, if the string still contains non-numeric characters, the string will be converted to NaN. For example, "3 m" is converted to NaN.

Instance:

The Code is as follows:


Console. log (10 * "3"); // 30
Console. log (10*"3 m"); // NaN, "3 m" will be converted to NaN

Value type conversion and Comparison

In JavaScript, the use of equal operators (=) involves value type conversion: If the value types on both sides of the = operator are inconsistent, then JS will convert them into consistent types and then judge them. Note that two values of different types may be equivalent after type conversion, but this does not mean that the result of using the = Operator for them must be true. A simple example is undefined and false: After undefined is converted to a boolean type, the result is exactly false, but in fact the result of undefined = false is false.

Explicit type conversion

Automatic conversion of JavaScript types is very convenient, but it also brings problems such as code maintainability. To make the program code clearer and reduce ambiguity, explicit type conversion is sometimes used in JS programs:

The Code is as follows:


Number ("3") // 3
String (false) // "false"
Boolean ([]) // true


In most cases, the explicit type conversion result is the same as the JS automatic type conversion result. However, there is a special case: When null or undefined is automatically converted to an Object, JS will throw a TypeError, but when explicitly converting null or undefined into an Object, JS will return an empty Object:

The Code is as follows:


Console. log (Object (undefined); // empty object
Console. log (Object (null); // empty object


At the same time, if the type specified for Explicit conversions is different from that specified for automatic JS conversions, the results will be different. For example, the undefined = false result mentioned earlier is false. If the explicitly specified conversion type is Boolean, the result is true:

The Code is as follows:


Console. log (undefined = false); // false
Console. log (Boolean (undefined) = Boolean (false); // true

Use of automatic type conversion

In JS, automatic conversion of value types can be used to achieve the same effect as explicit conversion through operators, such:

The Code is as follows:


Console. log (false + ""); // "false"
Console. log (+ false); // 0
Console. log (!! 3); // true

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.