Summary of common JS type conversion methods

Source: Internet
Author: User

I. type conversion

1. Convert to string

Ecmascript's boolean values, numbers, and string values are interesting because they are pseudo objects, which means they actually have attributes and methods.
For example:

JSCode Copy codeThe Code is as follows: var scolor = "blue ";
Alert (scolor. Length); // outputs "4"
VaR scolor = "blue ";
Alert (scolor. Length); // outputs "4"

All in all, the three main primitive values, Boolean values, numbers, and strings all have the tostring () method. All objects defined by ecmascript have the tostring () method, whether it is a pseudo object or a real object.

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

JS CodeCopy codeThe Code is as follows: var bfound = false;
Alert (bfound. tostring (); // outputs "false"
VaR bfound = false;
Alert (bfound. tostring (); // outputs "false"

The tostring () method of the number type is special. It has two modes: default mode and base mode. The default mode and tostring () mode are used () the method outputs the numeric value (whether it is an integer, floating point number, or scientific Notation) with the corresponding string ).
JS CodeCopy codeThe Code is as follows: var inum1 = 10;
VaR fnum2 = 10.0;
Alert (inum1.tostring (); // outputs "10"
Alert (fnum2.tostring (); // outputs "10"
VaR inum1 = 10;
VaR fnum2 = 10.0;
Alert (inum1.tostring (); // outputs "10"
Alert (fnum2.tostring (); // outputs "10"

The base mode of the tostring () method of the number type can be used to output numbers with different base (base) numbers.
JS CodeCopy codeCode: var inum = 10;
Alert (inum. tostring (2); /// outputs "1010"
Alert (inum. tostring (8); // outputs "12"
Alert (inum. tostring (16); // outputs ""
VaR inum = 10;
Alert (inum. tostring (2); /// outputs "1010"
Alert (inum. tostring (8); // outputs "12"
Alert (inum. tostring (16); // outputs ""

2. convert to a number

Ecmascript provides two methods to convert non-numeric values into numbers: parseint () and parsefloat ().
Note: Only when these methods are called for the string type (except number) can Nan be returned for other types be correctly run.

For example:
JS CodeCopy codeCode: var inum1 = parseint ("1234 blue"); // returns 1234
VaR inum2 = parseint ("OXA"); // returns 10
VaR inum3 = parseint ("22.5"); // returns 22
VaR inum4 = parseint ("blue"); // returns Nan
VaR inum1 = parseint ("1234 blue"); // returns 1234
VaR inum2 = parseint ("OXA"); // returns 10
VaR inum3 = parseint ("22.5"); // returns 22
VaR inum4 = parseint ("blue"); // returns Nan

The parseint () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to a decimal integer. The second parameter specifies which method to parse.
JS CodeCopy codeCode: var inum1 = parseint ("af", 16); // returns 175
VaR inum2 = parseint ("10", 2); // returns 2
VaR inum3 = parseint ("10", 8); // returns 8
VaR inum4 = parseint ("10", 10); // returns 10
VaR inum1 = parseint ("af", 16); // returns 175
VaR inum2 = parseint ("10", 2); // returns 2
VaR inum3 = parseint ("10", 8); // returns 8
VaR inum4 = parseint ("10", 10); // returns 10

Note: If the decimal number contains leading 0, it is best to use base 10; otherwise, the octal value is obtained.
JS CodeCopy codeCode: var inum1 = parseint ("010"); // returns 8
VaR inum2 = parseint ("010", 8); // returns 8
VaR inum3 = parseint ("010", 10); // returns 10
VaR inum1 = parseint ("010"); // returns 8
VaR inum2 = parseint ("010", 8); // returns 8
VaR inum3 = parseint ("010", 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. For this method, the first decimal point is a valid character. If two decimal points are used, the second decimal point is considered invalid. Another difference with this method is that the string must represent a floating point number in decimal format.

JS CodeCopy codeCode: var fnum1 = parsefloat ("1234 blue"); // returns 1234.0
VaR fnum2 = parsefloat ("0xa"); // returns Nan
VaR fnum3 = parsefloat ("22.5"); // returns 22.5
VaR fnum4 = parsefloat ("22.34.5"); // returns 22.34
VaR fnum5 = parsefloat ("0908"); // returns Nan
VaR fnum6 = parsefloat ("blue"); // returns Nan
VaR fnum1 = parsefloat ("1234 blue"); // returns 1234.0
VaR fnum2 = parsefloat ("0xa"); // returns Nan
VaR fnum3 = parsefloat ("22.5"); // returns 22.5
VaR fnum4 = parsefloat ("22.34.5"); // returns 22.34
VaR fnum5 = parsefloat ("0908"); // returns Nan
VaR fnum6 = parsefloat ("blue"); // returns Nan

3. forced type conversion
the following three types of forced type conversion are available in ecmascript:
(1 ). boolean (value)
converts a given value to the boolean type.
when the value to be converted is a string with at least one character, a non-zero number, or an object, the Boolean () function returns true. If the value is a Null String, number 0, undefined, or null, it returns false.
example:
JS Code copy Code the code is as follows: var b1 = Boolean (" "); // false;
var b2 = Boolean ("hi"); // true
var B3 = Boolean (100 ); // true
var B4 = Boolean (null); // false
var B5 = Boolean (0 ); // false
var B6 = Boolean (new object (); // true
var b1 = Boolean (""); // false;
var b2 = Boolean ("hi"); // true
var B3 = Boolean (100 ); // true
var B4 = Boolean (null); // false
var B5 = Boolean (0 ); // false
var B6 = Boolean (new object (); // true

(2). Number (value)
converts a given value to a number (which can be an integer or a floating point number ).
remember that the parseint () and parsefloat () methods only convert strings before the first invalid character, so "4.5.6" will be converted to "4.5 ". Use number () for forced type conversion. "4.5.6" will return Nan because the entire string value cannot be converted to a number. If the string can be fully converted, number () determines whether to call the parseint () method or the parsefloat () method.
example:
JS Code copy Code the code is as follows: 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
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

(3). String (value)
converts a given value to a string.
the only difference from calling the tostring () method is that forced type conversion for null or undefined values can generate a string without causing an error:
JS Code copy Code the code is as follows: var S1 = string (null); //" null "
var onull = NULL;
var S2 = onull. tostring (); // causes an error

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.