Value type conversions in JavaScript Introduction to _javascript Tips

Source: Internet
Author: User
Tags numeric value string to number

When you perform + 、-、 *,/, = =,!=, and so on in JavaScript, if the value types on the left and right sides of the operator are inconsistent with the expected type, JavaScript converts the values on both sides of the operator to the desired type before doing so. When the expected value type is string, JavaScript converts the value to string, and when the expected value type is number, JavaScript converts the value to numbers (return Nan if it cannot be converted to a numeric value), such as:

Copy Code code as follows:

Console.log (+ "cats");//10 Cats
Console.log (* "Cats");//nan, "Cats" would 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

The value conversion rules in JavaScript can refer to table 3-2 in the book "Javascript–the Definitive Guide". JavaScript type conversions. Some of the more notable areas are:

1.undefined is converted to number and the result is Nan.
The result is 0 after converting 2.null to number.
3. The result is 0 when the empty string "" is converted to number.
4.-0 converted to string, the result is "0″."
5. The result is 0 when the empty array [] is converted to number.
6. An array of only one number member, such as [9], is converted to number and the result is the Numer value (9).

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

1.JavaScript removes the whitespace character at the beginning and end of the string and then converts it, so a string like "42" can be successfully converted to the number 42.

2. When you delete a white space character at the beginning and at the end, the string is converted to Nan if it still contains non-numeric characters. For example, "3 M" will be converted to Nan.

Instance:

Copy Code code as follows:

Console.log (10 * "3");//30
Console.log (* "3 M");//nan, "3 M" is converted to NaN

Value type conversions vs. comparisons

In JavaScript, the use of the equals operator (= =) involves value type conversions: If the value types on either side of the = = operator are inconsistent, JS converts them to a consistent type and then judges them. It is important to be careful that two different types of values may be equivalent after a type conversion, but that does not mean that the result of using the = = operator for them must be true. A simple example is undefined and false: When you convert a undefined to a Boolean type, the result is exactly false, but in fact the undefined==false result is false.

Explicit type conversions

Automatic conversion of types using JavaScript is convenient, but it can also easily lead to issues such as code maintainability. In order to make the program code clearer and reduce ambiguity, there are times when explicit type conversions are used in JS programs:

Copy Code code as follows:

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

In most cases, the result of an explicit type conversion is the same as the result of the JS automatic type conversion; But there is a special case: when null or undefined is automatically converted to object, JS throws a TypeError error , but when you explicitly convert null or undefined to object, JS returns an empty object:
Copy Code code as follows:

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

Also, if the type specified by the explicit conversion is different from the type specified by the JS Auto conversion, the result is not the same. For example, the Undefined==false result mentioned earlier is false, and if the conversion type is explicitly specified as Boolean, the result is true:
Copy Code code as follows:

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

Use of type auto-conversion

In JS, you can use the automatic conversion of a value type to achieve the same effect as an explicit conversion by using an operator, such as:

Copy Code code 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.