Summary of JavaScript type conversion

Source: Internet
Author: User
Tags constructor numeric object object

Number () is used to convert numbers, String () is used to convert strings, and Boolean () is used to convert Boolean values.

JavaScript data type

There are five data types that can hold values in JavaScript:

· String

· Number

· Boolean

· Object

· Function

There are three types of objects:

· Object

· Date

· Array

The other two data types cannot hold values:

· Null

· Undefined

Typeof operator

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

Example

Typeof "John" // return string
Typeof 3.14 // return number
Typeof NaN // return number
Typeof false // Return boolean
Typeof [1, 2, 3, 4] // returns the object
Typeof {name: 'John', age: 34} // returns the object
Typeof new Date () // returns the object
Typeof function () {}// return function
Typeof myCar // return undefined (if myCar is not declared)
Typeof null // returns the object

Note:

· The data type of NaN is number.

· The data type of the array is object.

· The date data type is object.

· The null data type is object.

· The data type of variables not declared is undefined.

Data type of typeof

The typeof operator is not a variable. Because it is an operator. Similarly, the operator (+-*/) does not have any data type.

The return value of typeof is a string.

Constructor attributes

The constructor attribute returns the constructors of all JavaScript variables.

Example

"John". constructor // return function String ()
(3.14). constructor // return function Number ()
False. constructor // return function Boolean ()
[1, 2, 3, 4]. constructor // returns the Array () function ()
{Name: 'John', age: 34}. constructor // returns the function Object ()
New Date (). constructor // return function Date ()
Function () {}. constructor // return Function function ()

You can check the constructor attribute to determine whether an object type is Array (including the keyword "Array "):
Example

Function isArray (myArray ){
Return myArray. constructor. toString (). indexOf ("Array")>-1;
}

Similarly, you can check the constructor attribute to determine whether an object type is Date (including the keyword "Date "):
Example

Function isDate (myDate ){
Return myDate. constructor. toString (). indexOf ("Date")>-1;
}

JavaScript type conversion

JavaScript variables can be converted to new variables of another data type:

· JavaScript functions

· Automatic conversion mechanism of JavaScript

Convert Numbers to Strings

The Global method String () can convert numbers to strings.

This function can be used in any type of number, literal, variable, or expression:
Example

String (x) // returns the String converted from the variable x.
String (123) // returns the String converted from the literal value 123.
String (100 + 23) // returns the String converted from the expression.

The toString () method of the Number object can also achieve the same effect.

Example

X. toString ()
(123). toString ()
(100 + 23). toString ()

Convert Booleans to Strings

The Global method String () can also convert booleans to strings.

String (false) // return "false"
String (true) // return "true"

The toString () method of the Boolean object can also achieve the same effect.

False. toString () // return "false"
True. toString () // return "true"

Convert Dates to Strings

The Global method String () can also be converted to strings.

String (Date () // return Jul 17 2014 15:38:19 GMT + 0200

The Date object toString () method can also achieve the same effect.
Example

Date (). toString () // return Jul 17 2014 15:38:19 GMT + 0200

Convert Strings to Numbers

The Global method Number () can be converted to strings as numbers.

A string containing numbers (such as "3.14") can be converted to a number (3.14 ).

The empty string is converted to 0.

Other values are converted to NaN (Not a number ).

Number ("3.14") // returns 3.14
Number ("") // return 0
Number ("") // return 0
Number ("99 88") // return NaN

Unary operator +

+ The operator can be used to convert a variable to a number:

Example

Var y = "5"; // y is a string
Var x = + y; // x is a number.

If the variable cannot be converted, it can still be converted to the numeric type, but the value is NaN (Not a number ):

Example

Var y = "John"; // y is a string
Var x = + y; // x is a number and its value is NaN.


Convert Booleans to Numbers

Number (false) // return 0
Number (true) // return 1

Convert Dates to Numbers

The Global method Number () can be converted to numbers.

D = new Date ();
Number (d) // returns 1404568027739

The getTime () method of the date object can achieve the same effect.

D = new Date ();
D. getTime () // return 1404568027739

Automatic type conversion

When JavaScript tries to operate on a "wrong" data type, it first tries to convert it to a "correct" data type.

The result is not necessarily what you expect:

5 + null // return 5 because null is converted to 0
"5" + null // return "5 null" because null is converted to "null"
"5" + 2 // return 52 because 2 is converted to "2"
"5"-2 // returns 3 because "5" is converted to 5
"5" * "2" // returns 10 because "5" and "2" are converted to 5 and 2

Automatically converted to string type

JavaScript can automatically call the toString () function of a variable. When it tries to print an object or variable:

Document. getElementById ("demo"). innerHTML = myVar;

// If myVar = {name: "Fjohn"}, call toString () to convert it to "[object Object]"
// If myVar = [1, 2, 3, 4], call toString () to convert it to "1, 2, 3, 4"
// If myVar = new Date (), call toString () to convert it to "Fri Jul 18 2014 09:08:55 GMT + 0200"

Numbers and Boolean types can also be converted, but are generally not obvious:

// If myVar = 123 // toString () is converted to "123"
// If myVar = true // toString () is converted to "true"
// If myVar = false // toString () is converted to "false"

JavaScript type conversion table

This table shows the values after converting different JavaScript variables to Number, String, and Boolean:

Original value

Convert to numeric type

Convert to string type

Convert to Boolean type

False

0

"False"

False

True

1

"True"

True

0

0

"0"

False

1

1

"1"

True

"0"

0

"0"

True

"1"

1

"1"

True

NaN

NaN

"NaN"

False

Infinity

Infinity

"Infinity"

True

-Infinity

-Infinity

"-Infinity"

True

""

0

""

False

"20"

20

"20"

True

"Twenty"

NaN

"Twenty"

True

[]

0

""

True

[20]

20

"20"

True

[10, 20]

NaN

"10, 20"

True

["Twenty"]

NaN

"Twenty"

True

["Ten", "twenty"]

NaN

"Ten, twenty"

True

Function (){}

NaN

"Function (){}"

True

{}

NaN

"[Object Object]"

True

Null

0

"Null"

False

Undefined

NaN

"Undefined"

False


The value in the quotation marks indicates the string type.

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.