Type Conversion in JavaScript


In the previous tutorial, you learned about JavaScript Math Object in which you learned about trigonometric calculations in JavaScript as well as you have also learned about some other mathematical functions in JavaScript like getting a random number, finding maximum and minimum from a list of numbers. In this tutorial, you will learn about type conversion in JavaScript. Today we will explain how to convert the data type of any value.

Type Conversion in JavaScript

When we deal with JavaScript, we say it’s not a typed language like in other languages when we declare a variable we have to write its data type like

But when we deal with JavaScript we use only one keyword “var” which indicates that there is a variable declared here and we will store some data in it, and JavaScript automatically creates its data type. For example

Now the above line of code is just a variable, and it has no type, and we are not restricted to save some specific type of value in it. For example, if we write like:

Then the number variable will have integer data type.

Now this number variable has string data type.

Data types in JavaScript:

As we say above we can save any value in the declared variables, so there are five possible types of the data type in JavaScript of which the variables contain values.

  • string
  • Boolean
  • object
  • Object
  • Date
  • Array
  • Function
  • number

Type of Operators in JavaScript:

As we don’t know what is the type of variable so to find out the type of any variable we use the typeof operator. The typeof operator is an operator same like +, -, * but it returns string value which contains the name of the data type for any specific variable.


But when we run the above code it well alert string on every data type because when we get value in JavaScript, it will take all the data as string so to check the best result of type of operator, see example below:

Example Code:


typeof "Usman" is = string
typeof 23.1 is = number
typeof NaN is = number
typeof false is = boolean
typeof Array is = object
typeof Object is = object
typeof Date is = object
typeof Fnction is = function
typeof undefined varieble is = undefined
typeof null is = object


Type Conversion:

There are two types of data type conversion in JavaScript.

  • One made automatically
  • Other made using JavaScript functions

JavaScript parseInt()

In JavaScript, you can parse values as in other languages. We have already discussed type casting in Java and C#. The syntax for parsing a string to integer in javaScript is similar to the syntax in Java.

radix is a number between 2 and 36. You must specify radix as 10 for the decimal number system. If your string starts with “0x” then the radix is 16 as for the hexadecimal number.You can give spaces at the start of the string and at the end of the string but the spaces inside the string are not allowed. If there is a space after a number in the string then it will return only the first number in the string.




In case if you want to enter any special character like + – ) # etc. then it is also not allowed in JavaScript parseInt().  If the first character in the string can not be converted to an integer then it will return NaN.


JavaScript parseFloat()

parseFloat() converts a string to floating point number. Like parseInt, if parseFloat() can parse the first number it will convert it otherwise it will return NaN, leading and trailing brackets are also allowed.


Numbers to Strings:


This function is used to convert number type to string type.

The toString() function also converts same like the simple string function converts.

Boolean to String:

As there are two types of Boolean value, either true or false so we can convert this true and false into the string so that they act as a string, not a Boolean value.

Date to String:

This function converts Date object to the string so that we cannot do any calculation on it using date functions.

String to Number:

The number () function is used to convert any string to numeric data type, but the string must contain digits, not alphabets.

Boolean to Number:

To convert boolean to number, use number() function it will convert true into 1 and false into 0.