JS data type conversion

Source: Internet
Author: User

Javascript data type conversion methods include conversion functions, forced type conversion, and weak type conversion using JS variables.

1. conversion functions:

JS provides two conversion functions: parseint () and parsefloat. The former converts the value to an integer, and the latter converts the value to a floating point number. The two functions can run correctly only when these methods are called for the string type. Nan (not a number) is returned for other types ).

Before determining whether a string is a numeric value, parseint () and parsefloat () will carefully analyze the string. The parseint () method first checks the character at the position 0 and determines whether it is a valid number. If not, the method returns Nan and does not continue to perform other operations. If the character is a valid number, this method will view the characters at position 1 for the same test. This process continues until a non-valid number is found. At this time, parseint () converts the string before this character into a number.

For example: 

Parseint ("1234 blue"); // return 1234

Parseint ("22.5"); // return 22 for integers, the decimal point is invalid characters

Parseint ("blue"); // return Nan

The parseint () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer. The base is specified by the second parameter of the parseint () method.

For example:

Parseint ("af", 16); // return 175 hexadecimal

Parseint ("10", 2); // return 2 binary

Parseint ("10", 8); // return 8

Parseint ("10", 10); // return 10 decimal

If the decimal number contains the leading 0, it is best to use base 10 to avoid unexpected octal values.

For example:

Parseint ("010"); // return 8

Parseint ("010", 8); // return 8

Parseint ("010", 10); // return 10

The parsefloat () method is similar to the parseint () method, but there are differences. If the string contains two decimal places, the second decimal point is considered invalid, that is, the string "22.34.5" will be parsed to 22.34. Another difference is that the string must be in decimal format and cannot be octal or hexadecimal. Parsefloat () does not have the base mode.

For example:

Parsefloat ("1234 blue"); // return1234.0

Parsefloat ("22.34.5"); // return 22.34

Parsefloat ("0908"); // return 908

Parsefloat ("blue"); // return Nan

2. Forced type conversion

Boolean (value) -- converts a given value to a boolean type. If the converted value is a string of at least one character, a non-zero number, or an object, true is returned; if the value is a Null String, number 0, undefined, or null, false is returned.

Boolean (""); // return false

Boolean ("hi"); // return true

Boolean (100); // return true

Boolean (null); // return false

Boolean (0); // return false

Boolean (new object (); // return true

Number (value) -- converts a given value to a number (integer or floating point number). If the converted value is not a value, Nan is returned.

Number (false); // return 0

Number (true); // return 1

Number (undefined); // return Nan

Number (null); // return 0

Number ("5.5"); // return 5.5.

Number ("56"); // return 56

Number ("5.6.7"); // return Nan

Number (new object (); // return Nan

String (value) -- converts a given value to a string.

3. Weak type conversion of JS Variables

Bytes ------------------------------------------------------------------------------------

original link: http://www.cnblogs.com/446557021/archive/2011/10/13/2211043.html

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.