In  this article I am going to write about converting a javascript variable to integer. Its important to have both variable as integer when you’re doing calculations using javascript. I’ll share 2 ways here in this article, there might be other ways as well. Please feel free to share your favourite way in comments section.

Ok, Let’s start with an example:

As you can see in the example, I have 2 variables where one is string and another one is integer. And in the 3rd variable, I am doing an addition of the first 2 variables. Now, If you run this code, it will output 99 instead of 18 !

But you need it to provide 18 as output. Let’s have a look at the ways to do that.

Way 1:

This is the simplest way for converting a javascript variable to integer. What you need to do is just put a + before the string variable. Just like the following:

This code will output 18 which is perfect! Now let’s have a look at the other way.

Way 2:

You can use parseInt() function to convert string variable to integer. Let’s see how:


It will also return 18!

Pretty simple, wasn’t it ? 😉