JavaScript advanced programming Reading Notes (4) type conversion in ECMAScript _ javascript skills

Source: Internet
Author: User
In ECMAScript, if you are studying js, You can see 2.7 type conversion.
1. convert to a string
All objects can be converted to strings using the toString () method. Note that the toString () method of the Number type has two modes: default mode and base mode, the default toString () method is used to output the corresponding 10-digit numeric value with the corresponding string. Different base numbers can be output in the base mode. Example:

The Code is as follows:


Var iNum1 = 10;
Var iNum2 = 10.0;
Var iNum3 = 10;
Alert (iNum1.toString (); // outpus "10"
Alert (iNum2.toString (); // outpus "10"
Alert (iNum3.toString (2); // outpus "1010"
Alert (iNum3.toString (8); // outpus "12"
Alert (iNum3.toString (16); // outpus ""


2. convert to a number
ECMAScript provides two methods to convert non-numeric original values into numbers: parseInt () and parseFloat (). Note that parseInt () is character-by-character conversion, until it is a non-numeric character. The example program is as follows:

The Code is as follows:


Var iNum1 = parseInt ("1234 blue"); // returns 1234
Var iNum2 = parseInt ("0xA"); // 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 an integer. The base is specified by the second parseInt () parameter. The example is as follows:

The Code is as follows:


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 has a leading 0, the base mode is used.
Var iNum5 = parseInt ("010"); // returns 8
Var iNum6 = parseInt ("010", 8); // returns 8
Var iNum7 = parseInt ("010", 10); // returns 10


ParaseFloat () does not have a base mode. The others are roughly the same as parseInt (). For example:

The Code is as follows:


Var fNum1 = paraseFloat ("1234 blue"); // returns 1234.0
Var fNum2 = paraseFloat ("0xA"); // returns NaN
Var fNum3 = paraseFloat ("22.5"); // returns 22.5
Var fNum4 = paraseFloat ("22.34.5"); // returns 22.34
Var fNum5 = paraseFloat ("0908"); // returns 908
Var fNum6 = paraseFloat ("blue"); // returns NaN


3. Forced type conversion
The mandatory conversions available in ECMAScript are as follows:
Boolean (value) -- converts a given value to a Boolean value.
Number (value) -- converts a given value to a Number (which can be an integer or floating point Number)
String (value) -- converts a given value to a String.
Example:

The Code is as follows:


Var b1 = Boolean (""); // false-empty string
Var b2 = Boolean ("hi"); // 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
Var i1 = Number (false); // 0
Var i2 = Number (true); // 1
Var i3 = Number (undefined); // NaN
Var i4 = Number (null); // 0
Var f5. = Number ("5.5"); // 5.5
Var i6 = Number ("56"); // 56
Var i7 = Number ("5.6.7"); // NaN
Var i8 = Number (new Object (); // NaN
Var i9 = Number (100); // 100.


Author: tianxingjian, self-improvement
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.