JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

How To Convert Data Types in JavaScript

Type Conversion is a way to convert one data-type into another data-type.

In JavaScript, data types are used to classify one particular type of data, determining the values that you can assign to the type and the operations you can perform on it.

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 can help you to find the type of your variable.

The typeof operator returns the type of a variable or an expression.

Run code

You can use the typeof operator to find the data type of a JavaScript variable.

Implicit Conversion

As a programming language, JavaScript is very tolerant of unexpected values. Because of this, JavaScript will attempt to convert unexpected values rather than reject them outright. This implicit conversion is known as type coercion.

For example, in mathematical operations values are automatically converted to numbers.

However, the result is not always what you expected:

Run code

There are situations when we need to explicitly convert a value from one data type to another.

JavaScript provides a number of different methods to perform such data type conversion task.

In the following sections we will discuss those methods in detail.

Converting Values to Strings

Values can be explicitly converted to strings by calling either String() function or x.toString() method.

With the String() function, let’s convert a Boolean value to a string by passing the value true into the parameter:

Run code

Alternatively, we can pass a number into the function:

Run code

We can use the typeof operator to check for type:

Run code

We can use x.toString() method in a similar way. We can replace x with a variable:

Run code

Alternatively, we can put a value within parentheses rather than a variable with x.toString():

Run code

By using String() or x.toString() we are able to explicitly convert values of Boolean or number data types to string values in order to ensure that our code behaves as we anticipate.

Converting Values to Numbers

When converting values to a number data type, we’ll use the Number() function.

Primarily, we’ll be converting strings of numerical text to numbers, but we can also convert Boolean values.

We can pass a string of a number to the Number() function:

Run code

We can also assign a string to a variable and then convert it:

Run code

Strings of white spaces or empty strings will convert to 0 (zero):

Run code

Be aware that strings of non-numbers will convert to NaN which stands for Not a Number. This includes numbers separated by spaces:

Run code

For Boolean data types, false will evaluate to 0 and true will evaluate to 1:

Run code

If the parameter is a Date object, the Number() function returns the number of milliseconds since midnight January 1, 1970 UTC:

Run code

The Number() function converts non-number data types to numbers.

Converting Values to Booleans

To convert numbers or strings to Boolean values, the Boolean() function is used.

Any value that is interpreted as empty, like the number 0, an empty string, or values that are undefined or NaN or null are converted to false:

Run code

Other values will be converted to true, including string literals composed of white space:

Run code

Note that "0" as a string literal will convert to true since it is a non-empty string value:

Run code

Converting numbers and strings to Boolean values can allow us to evaluate data within binary terms and can be leveraged for control flow in our programs.

Converting Arrays to Strings

The toString() method converts an array to a string of (comma separated) array values.

Run code

The join() method also converts all the elements of an array into a new string.

This method behaves just like toString(), but in addition you can specify the separator.

Run code

JavaScript automatically converts an array to a comma separated string when a primitive value is expected.

This is always the case when you try to output an array.

Run code

Converting Strings to Arrays

The split() method splits a string into an array of substrings, and returns the new array.

We will use the split() method to separate the array by a whitespace character, represented by " ".

Run code

Now that we have a new array in the arr variable, we can access each element with an index number:

Run code

In the following example we will use "i" as a separator:

Run code

If an empty string ("") is used as the separator, the string is converted to an array of characters:

Run code