Home > Web Front-end > JS Tutorial > Summary of common methods of JS type conversion_javascript skills

Summary of common methods of JS type conversion_javascript skills

WBOY
Release: 2016-05-16 18:26:03
Original
1309 people have browsed it
1. Type conversion

1. Convert to string

The interesting thing about ECMAScript’s Boolean values, numbers and primitive values ​​of strings is that they are pseudo-objects. This means they actually have properties and methods.
For example:

Js code
Copy code The code is as follows:

var sColor = "blue";
alert(sColor.length);//outputs "4"
var sColor = "blue";
alert(sColor.length);//outputs "4"


In short, the three main primitive values ​​Boolean values, numbers and strings have toString() method. All objects defined by ECMAScript have a toString() method, whether it is a pseudo object or a real object.

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

Js code
Copy code The 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 There are three modes, namely the default mode and the base mode. In the default mode, the toString() method just outputs the numeric value (whether it is an integer, floating point number or scientific notation) with the corresponding string.
Js code
Copy code The 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"


Using the base mode of the toString() method of the Number type, numbers can be output using different bases (bases).
Js code
Copy code The code is 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 number

ECMAScript Two methods are provided to convert non-numeric primitive values ​​into numbers, namely parseInt() and parseFloat().
Note: Only when these methods are called on the String type (except Number) can they run correctly. NaN will be returned for other types.

For example:
Js code
Copy code The code is 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


parseInt() method There is also base mode, which can convert binary, octal, hexadecimal or any other base string into a decimal integer. The second parameter specifies which base to parse.
Js code
Copy code 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
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 0s, it is best to use base 10, otherwise the value obtained will be octal.
Js code
Copy code The code is as follows:

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, starting from position 0 and looking at each character until it is found to the first non-valid character, and then convert the string before that character into numbers. For this method, the first decimal point is a valid character. If two decimal points are used, the second decimal point will be 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 The code is 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. Forced type conversion
The three types of forced type conversion available in ECMAScript are as follows:
(1).Boolean(value)
Convert the given value into Boolean type.
The Boolean() function will return true when the value to be converted is a string, non-zero number or object with at least one character. If the value is an empty string, the number 0, undefined or null, it will return false.
For 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 the given value to a number (can be an integer or a floating point number).
Remember that the parseInt() and parseFloat() methods only convert the string before the first invalid character, so "4.5.6" will be converted to "4.5". Casting with Number(), "4.5.6" will return NaN because the entire string value cannot be converted to a number. If the string can be completely converted, Number() will determine whether to call the parseInt() method or the parseFloat() method.
For 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)
Convert the given value into a string.
The only difference from calling the toString() method is that casting a null or undefined value can generate a string without raising 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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template