Common methods for JS type conversion Summary _javascript tips

Source: Internet
Author: User
Tags numeric
One, type conversion

1. Convert to String

The interesting thing about ECMAScript's Boolean values, numbers, and the original values of strings is that they are pseudo objects, which means they actually have properties and methods.
Such as:

JS Code
Copy Code code as follows:

var scolor = "Blue";
alert (scolor.length);//outputs "4"
var scolor = "Blue";
alert (scolor.length);//outputs "4"


All in all, 3 of the primary values Boolean values, numbers, and strings have the ToString () method. All objects defined by ECMAScript have the ToString () method, whether it is a pseudo object or a true object.

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

JS Code
Copy Code code 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, the default mode and the base mode, the default mode, and the ToString () method simply outputs numeric values (whether integers, floating-point numbers, or scientific notation) with the corresponding string.
JS Code
Copy Code code 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"


Using the base mode of the number type ToString () method, you can output numbers in different bases (in radix).
JS Code
Copy Code code as follows:

var inum = 10;
Alert (inum.tostring (2)); Outputs "1010"
Alert (inum.tostring (8)); Outputs "12"
Alert (inum.tostring (16)); Outputs "A"
var inum = 10;
Alert (inum.tostring (2)); Outputs "1010"
Alert (inum.tostring (8)); Outputs "12"
Alert (inum.tostring (16)); Outputs "A"


2. Convert to Digital

ECMAScript provides two methods for converting a non-numeric original value into a number, namely parseint () and parsefloat ().
Note: These methods are called only for string type (except number) to correctly run Nan for other types.

For example:
JS Code
Copy Code code as follows:

var iNum1 = parseint ("1234blue");//returns 1234
var iNum2 = parseint ("Oxa"); Returns 10
var iNum3 = parseint ("22.5"); Returns 22
var iNum4 = parseint ("Blue"); Returns NaN
var iNum1 = parseint ("1234blue");//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 a base pattern that converts binary, octal, hexadecimal, or any other string of strings into decimal integers. The second parameter specifies which system to parse.
JS Code
Copy Code code as follows:

var iNum1 = parseint ("AF";)//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";)//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 a leading 0, it is best to use cardinality 10, otherwise the octal value is obtained.
JS Code
Copy Code code as follows:

var iNum1 = parseint ("010"); Returns 8
var iNum2 = parseint ("010", 8); Returns 8
var iNum3 = parseint ("010");//returns 10
var iNum1 = parseint ("010"); Returns 8
var iNum2 = parseint ("010", 8); Returns 8
var iNum3 = parseint ("010");//returns 10


The Parsefloat () method works like the parseint () method, viewing each character starting at position 0 until the first inactive character is found, and then converting the string before the character to a number. For this method, the first decimal point that appears is a valid character. If you use two decimal points, the second decimal point is considered invalid. Another difference in using this method is that the string must represent a floating-point number in decimal form.

JS Code
Copy Code code as follows:

var fNum1 = parsefloat ("1234blue"); 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 ("1234blue"); 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. Force type Conversions
The 3 mandatory type conversions available in ECMAScript are as follows:
(1). Boolean (value)
Converts a given value to a Boolean.
The Boolean () function returns True when the value to be converted is a string of at least one character, a number other than 0 digits, or an object. If the value is an empty string, number 0, undefined, or null, it returns FALSE.
Such as:
JS Code
Copy Code code as follows:

var B1 = Boolean (""); False
var b2 = Boolean ("Hi");//true
var B3 = Boolean (m);//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 (m);//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 (can be an integer or floating point count).
Remember that the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6" is converted to "4.5". Force type conversion with number (), and "4.5.6" returns Nan, because the entire string value cannot be converted to numbers. If the string can be completely converted, number () will determine whether to call the parseint () method or call the Parsefloat () method.
Such as:
JS Code
Copy Code code 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 invoking the ToString () method is that coercion of a type cast on a null or undefined value can generate a string without raising an error:
JS Code
Copy Code code 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.