Javascript series: ecmascript type conversion

Source: Internet
Author: User
Tags type casting

1. convert to a string

The boolean values, numbers, and string values of ecmascript are pseudo objects, that is, they all have attributes and methods.

 
VaRColor = 'Blue'; Alert (color. Length );//Output "4"

The three main primitive values, Boolean values, numbers, and strings, all have the tostring () method, which can be converted into strings.

The tostring () method of the boolean type only outputs "true" and "false", and the result is determined by the value of the variable.

 
VaRB =False; Alert (B. tostring ());//Output "false"

The tostring () method of the number type is special. It has two modes: default mode and base mode. The default mode is used. The tosting () method only outputs numeric values using the corresponding string. In the default mode, No matter what representation method is used to declare a number, the tostring () method of the number type returns a decimal representation.

The base mode of the tostring () method of the number type can be used to output numbers using different bases. The base is just another name of the base to be converted to. It is a parameter of the tostring () method.

VaRI = 10; Alert (I. tostring (2 ));//Output "1010"Alert (I. tostring (8 ));//Output "12"Alert (I. tostring (16 ))//Output ""

2. convert to a number

Ecmascript provides two methods to convert non-numeric original values into numbers, namely parseint () and parsefloat (). The former converts the value to an integer, and the latter converts the value to a floating point number. Only the two methods of the string type can be correctly called, and Nan is returned for other types.

Before determining whether a string is a numeric value, parseint () and parsefloat () analyze the string carefully. The parseint () method first looks at the character from position 0 to determine whether it is a valid number. If not, the method returns Nan and does not continue to perform other operations. However, if the character is a valid number, this method will view the characters at position 1 and perform the same test. This process continues until a non-valid number is found. At this time, parseint () converts the string before the character to a number. For example, if you convert the string "1234 ABCD" to an integer, parseint () returns 1234. When the character 'a' is detected, the detection process is stopped. The number literal in the string is correctly converted to a number, so the string "0xa" is correctly converted to a number 10. However, the string "22.5" will be converted to 22.

VaRInum1 = parseint ('1234abcd ');//Returns 1234VaRInum2 = parseint ('0xa ');//Returns 10VaRInum3 = parseint ('22. 5 ');//Returns 22VaRInum4 = parseint ('Blue ');//Returns 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.

VaRInum1 = parseint ('af', 2 );//Returns 175VaRInum2 = parseint ('10', 2 );//Returns 2VaRInum3 = parseint ('10', 8 );//Returns 8VaRInum4 = parseint ('10', 10 );//Returns 10

The parsefloat () method is similar to the parseint () method. Each character is viewed from position 0 until the first invalid character is found, then convert the string before the character to a number. However, for this method, the first decimal point is a valid character. If there are two decimal points, the second decimal point will be considered invalid, parsefloat () to convert the string before the second decimal point to a number. For example, the string '12. 34.5 'is converted to 12.34.

The string of the parsefloat () method must be in decimal format, rather than octal or hexadecimal format. This method ignores the leading 0. For example, the octal number 0908 will be parsed to 908. For hexadecimal number 0xa, this method returns Nan, where X is not a valid character.

Parsefloat () does not have the base mode.

 
VaRFnum1 = parsefloat ('1234abcd ');//Returns 1234.0VaRFnum2 = parsefloat ('0xa ');//Returns NanVaRFnum3 = parsefloat ('22. 5 ');//22.5VaRFnum4 = parsefloat ('12. 34.5 ');//Returns 12.34VaRFnum5 = parsefloat ('20140901 ');//Returns 908VaRFnum6 = parsefloat ('abcd ');//Returns Nan

3. Forced type conversion

Type Casting is used to process the type of the converted value. The following are three types of forced conversions available in ecmascript:

Convert Boolean (value) -- converts a given value to a Boolean value.

Numeric number (value) -- converts a given value to a number (which can be an integer or floating point number)

Convert string (value) -- converts a given value to a string

The forced type conversion of number () is similar to that of parseint () and parsefloat (), except that it converts the entire value instead of the partial value.

 VaR B1 = Boolean (''); //  False-empty string  VaR B2 = Boolean ('abc '); //  True-not empty string  VaR B3 = Boolean (100 );//  True-not zero number  VaR B4 = Boolean ( Null ); //  False-Null  VaR B5 = Boolean (0 ); //  False-zero  VaR B6 = Boolean ( New Object ()); //  True-Object 
Number (False ) //  0 Number ( True ) //  1 Number (undefined) //  Nan Number ( Null ) //  0 Number ('5. 5 ') //  5.5 Number ('56 ')//  56 Number ('5. 6.7 ') //  Nan Number ( New Object ()) //  Nan Number (100) //  100 

The difference between string () and tostring () is that the forced type of null or undefined values can be converted to the production string without causing an error.

  var  S1 = string ( null ); ///  " null "  var  onull =  null  ;  var  S2 = onull. tostring (); ///   cause error  
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.