JavaScript type conversion

Javascript variables are loosely typed, which can store any data type supported by Javascript, and the type of its variables can be dynamically changed at runtime. Please see the example:


##var n = 10;

n = "hello CSSer !";

n = {};

In the above example, first declare the n variable and initialize its value to 10 (integer type), and then Assign the string "hello CSSer!" to n, then assign an object to it, and finally the type of n is the object type. It can be seen that the type of variable n is dynamic. In actual programming, we recommend not to change the type of variables frequently, because this is not good for debugging.


JavaScript Data Types

There are 5 different data types in JavaScript:

string

number

boolean

object

function

3 object types:

Object

Date

Array

2 data types that do not contain any value:

null

undefined

typeof operator

You can use typeof operator to check the data type of JavaScript variables.

    php.cn 

Please note:

The data type of NaN is number

The data type of array (Array) is object

The data type of date (Date) The data type of object

null is object

The data type of undefined variables is undefined

constructor property

constructor property returns the construction of all JavaScript variables function.

    php.cn 



Explicit conversion

By manually performing type conversion, Javascript provides the following Transformation function:

Convert to numeric type: Number(mix), parseInt(string,radix), parseFloat(string)

Convert to string type: toString(radix), String(mix)
Convert to Boolean type: Boolean(mix)

1. Number(mix) function can convert any type of parameter mix into a numerical type

The rules are:

If it is a boolean value, true and false are converted to 1 and 0 respectively

If it is a numeric value, return itself.

If it is null, return 0.

If it is undefined, return NaN.

If it is a string, follow the following rules:

1) If the string contains only numbers, convert it to decimal (leading 0 is ignored)

2) If the string contains a valid floating point format, convert it to a floating point value (ignoring leading 0s)

3) If it is an empty string, convert it to 0

4) If the string contains a format other than the above, convert it to NaN

If it is an object, call the valueOf() method of the object, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, call the object's toString() method and convert the returned string value again according to the previous rules.

Number("3.14") // Return 3.14
Number(" ") // Return 0
Number("") // Return 0
Number("99 88") // Return NaN


2, parseInt(string, radix) function, convert the string Convert to a numeric value of integer type. It also has certain rules:


Ignore spaces in front of the string until the first non-empty character is found

If the first If a character is not a numeric symbol or a negative sign, NaN is returned

If the first character is a number, parsing will continue until the string parsing is completed or a non-numeric symbol is encountered

If the above If the result of step parsing starts with 0, it will be parsed as octal; if it starts with 0x, it will be parsed as hexadecimal

If the radix parameter is specified, radix will be used as the base. Parsing

3. The parseFloat(string) function converts the string into a floating point type value

Its rules are basically the same as parseInt , but there are some differences: the first decimal point symbol in the string is valid, and parseFloat ignores all leading 0s. If the string contains a number that can be parsed as an integer, an integer value is returned instead of a floating point value.

4. toString(radix) method. All types of values except undefined and null have a toString() method, which returns the string representation of the object

Object Operation

Array Convert the elements of Array into strings. The resulting strings are comma separated and concatenated.

Boolean If the Boolean value is true, return "true". Otherwise, return "false".

Date Returns the text representation of the date.

Error Returns a string containing relevant error information.

Function returns a string in the following format, where functionname is the name of the called toString method function:

function functionname( ) { [native code] }

Number Returns the textual representation of a number.

String Returns the value of the String object.

Default returns "[object objectname]", where objectname is the name of the object type.

5. String(mix) function converts any type of value into a string. The rules are:

If there is a toString() method, call this method (without passing the radix parameter) and return the result

If it is null, return "null"

If it is undefined, returns "undefined"

    php.cn 

6. Boolean(mix) function, converts any type of value into a Boolean value.

The following values will be converted to false: false, "", 0, NaN, null, undefined, and any other values will be converted to true.


Example:

    php中文网(php.cn)  




##Unary operator+

Operator + can be used to convert variables to numbers :

    php.cn 


Continuing Learning
||
php.cn

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!