JavaScript Type Conversion


Number() converts to a Number, String() converts to a String, Boolean() converts to a Boolean.


JavaScript Data Types

In JavaScript there are 5 different data types that can contain values.

  • string
  • number
  • boolean
  • object
  • function

There are 3 types of objects.

  • Object
  • Date
  • Array

And 2 data types that cannot contain values.

  • null
  • undefined

The typeof Operator

The typeof operator to find the data type of a JavaScript variable.

typeof "Sachin"                  // Returns string
typeof 3.14                      // Returns number
typeof NaN                       // Returns number
typeof false                     // Returns boolean
typeof [2,4,6]                   // Returns object
typeof {name:'Sachin', age:19}   // Returns object
typeof new Date()                // Returns object
typeof function () {}            // Returns function
typeof Bike                      // Returns undefined (if Bike is not declared)
typeof null                      // Returns object
Note:
  • The data type of NaN is number
  • The data type of an array is object
  • The data type of a date is object
  • The data type of null is object
  • The data type of an undefined variable is undefined

JavaScript Type Conversion

JavaScript variables can be converted to a new variable and another data type.

  • By the use of a JavaScript function
  • Automatically by JavaScript itself

Converting Numbers to Strings

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

It can be used on any type of numbers, literals, variables, or expressions.

<span id="view"><⁄span>
<span id="view2"><⁄span>
<span id="view3"><⁄span>

<script type="text/javascript">
var x = 2246;
document.getElementById("view").innerHTML = String(x); 
document.getElementById("view2").innerHTML = String(2246); 
document.getElementById("view3").innerHTML = String(2000 + 246);
</script>
Output



The Number method toString() does the same.

<span id="view"><⁄span>
<span id="view2"><⁄span>
<span id="view3"><⁄span>

<script type="text/javascript">
var x = 2246;
document.getElementById("view").innerHTML = toString(x); 
document.getElementById("view2").innerHTML = toString(2246); 
document.getElementById("view3").innerHTML = toString(2000 + 246);
</script>
Output



MethodDescription
toExponential()Returns a string, with a number rounded and written using exponential notation.
toFixed()Returns a string, with a number rounded and written with a specified number of decimals.
toPrecision()Returns a string, with a number written with a specified length

Converting Booleans to Strings

The global method String() can convert booleans to strings.

Example

String(false)        // returns "false"
String(true)         // returns "true"

The Boolean method toString() does the same.

Example

false.toString()     // returns "false"
true.toString()      // returns "true"

Converting Dates to Strings

The global method String() can convert dates to strings.

Example

String(Date())   // returns Sun Jan 11 2015 12:10:00 GMT+0530 (India Standard Time)

The Date method toString() does the same.

Example

Date().toString()   // returns Sun Jan 11 2015 12:10:00 GMT+0530 (India Standard Time)

Converting Strings to Numbers

The global method Number() can convert strings to numbers.

Strings containing numbers ("3.14") convert to numbers (3.14).

Empty strings convert to 0.

Anything else converts to NaN (Not a number).

Example

Number("3.14")    // returns 3.14
Number(" ")       // returns 0
Number("")        // returns 0
Number("99 88")   // returns NaN
MethodDescription
parseFloat()Parses a string and returns a floating point number
parseInt()Parses a string and returns an integer

Converting Booleans to Numbers

The global method Number() can also convert booleans to numbers.

Example

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

Converting Dates to Numbers

The global method Number() can be used to convert dates to numbers.

Example

x = new Date();
Number(x)          // returns 13205473236886

The date method getTime() does the same.

Example

x = new Date();
x.getTime()          // returns 13205473236886

Unary + Operator

The unary + 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

Automatic Type Conversion

When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type.

Example

2 + null    // returns 2         because null is converted to 0
"2" + null  // returns "2null"   because null is converted to "null"
"2" + 2     // returns "21"      because 1 is converted to "1" 
"2" - 1     // returns 1         because "2" is converted to 2

Share this article on