tutorialstonight logo

JAVASCRIPT TYPE CONVERSION


In JavaScript, operators and function automatically converts the values given to them to the right type.

Example, 2 * "5" outputs 10, 2 + true outputs "2true", alert() method converts any value to string before outputing.

However, there are also many cases when we have to convert data type explicitly to our desired type.

In this tutorial, we will see types of type conversion and how to convert data from one type to another type.


The typeof Operator

Before going to type conversion, you must know about the typeof operator.

The typeof is used to get the data type of JavaScript variables and values. It returns the data type in form of a string value.

▶ Try It

You can see in the output the type of array is object.

Note: The typeof operator can't be used to determine if a JavaScript object is an array or not. To determine if an object is an array you may use isArray method.

Type Conversion

There are 2 types of type conversion in JavaScript:

  1. Implicit Conversion - Type conversion that is made automatically when passing to or returning from a functionType call.
  2. Explicit Conversion - Type conversion that is made manually by using some function or method


Implicit type conversion

There are some cases when JavaScript automatically converts one data type to another data type, this automatic conversion of data type is known as implicit conversion.

Here are examples of the cases when JavaScript implicitly converts one data type to another.

Example 1: Implicit conversion to number

The values in the example below are converted to number implicitly to perform following JavaScript operations.

▶ Try It

"5" at line 1 is a string which is converted to number for performing subtraction with 2, and same in lines below it. Also boolean is converted to number at line number 5 and 6.

Example 2: Implicit conversion to string

The values in the example below are converted to string implicitly to perform following JavaScript operations.

▶ Try It

In the above example, 2 is converted to a string and is concatenated with "5". Same in the lines below operands are converted to a string and concatenated.


Explicit conversion

In JavaScript, you can convert one data type to another depending on your use case. Example:

  • Adding 2 number which is in string form. "5" + "5"
  • Converting a string to date. "Mon Nov 1997"
  • Converting boolean to number. true -> 1
etc.

The type conversion that is explicitly done is known as explicit conversion. There are some built-in methods in JavaScript for such conversion.


JavaScript Convert Number to String

There are 3 ways by which you can converts number to string in JavaScript.

  1. Using String() method
  2. Using toString() method
  3. Using trick

1. convert number to string using String() method

To convert a number to string use the global method String(). It returns the string form of passed value.

The String() method can convert any type of numbers, variables, literals and expression to a string.

▶ Try It

2. convert number to string using toString() method

The toString() does the same task, it converts number to string.

▶ Try It

3. convert number to string using trick

Alternatively, you can convert a number to a string by adding the number with an empty string but make sure the empty string comes first in addition and number in the last.

Means, if you want to convert 10 to string then do "" + 10 not 10 + "".

If you want to convert an expression to string then wrap the expression in the small bracket, so that expression is first solved then converted to a string. See line number 4.

▶ Try It

Convert String to Number JavaScript

In JavaScript, there are 2 main ways by which you can convert a string to number:

  1. Using Number() method
  2. Using + operator

There are another 2 ways but they are for special cases parseInt() and parseFloat() method.


1. Convert String to Number: Number() method

To convert string to number use the global method Number().

Pass the string as an argument to the method. Example Number("10") method will convert "10" to 10.

An empty string is converted to 0 and the value that can not be converted to number converts to NaN (Not a Number).

▶ Try It

2. Convert String to Number: unary + operator

The unary + operator can be used to convert a string to number.

Just place a plus (+) operator in front of the string. It also looks clean.

If the string can not be converted to a number then it will be converted to NaN.

▶ Try It

convert date to string

To convert Date object to string simply use String() and toString() method discussed above or add an empty string to the date object.

▶ Try It

convert date to number in javascript

When a Date object is converted to a number then it returns a number of milliseconds since January 1, 1970, UTC.

To convert date object to number you can use Number() and getTime() method.

▶ Try It

Convert values to boolean

You can convert values to boolean in 2 different ways:

  1. Using Boolean() method
  2. Using the negation symbol (!)

Boolean() method

To convert the number or string to boolean use Boolean() method.

In JavaScript, 0, null, undefined, ' ' and NaN is converted to false.

▶ Try It

Using negation (!)

To convert a string or number to boolean use the negation (!) symbol if the value is falsy then it will be converted to false else true.

▶ Try It

JavaScript Type Conversion Table

Here is a table which represents what will be the value of a variable when converted to another data type.

Value To Number To String To Boolean
0 0 "0" false
1 1 "1" true
"0" 0 "0" false
"1" 1 "1" true
"John" NaN "john" true
true 1 "true" true
false 0 "false" false
'' 0 "" false
undefined 0 "undefined" false
null 0 "null" false
NaN 0 "NaN" false
{} NaN "[object, object] true
[] 0 "" false
[1, 2, 3] NaN "1,2,3" true
function() { } NaN "function() { }" true