By Bobby Iliev
Hi all,
I wanted to create this question/answer on how to convert a string to an integer in JavaScript as this is something that I often get asked a lot about.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Hi all,
For everyone looking for a quick answer, here is how you could convert a string to an integer in Javascript with parseInt()
:
var a = "20";
var b = parseInt(a);
// Check the type of b:
typeof b
//Output
'number'
// Check the type of a
typeof a
'string'
For anyone who wants to learn more about this, feel free to continue reading!
JavaScript is one of, if not the most famous and well-known programming languages right now. JavaScript has 7 data types, but the two most simple and common data types are strings
and numbers
. If we have a variable with the value '5'
, that is a string, but if our value is 5
, then we have a number. Note that the string values have to be put in quotes.
Let’s say that we want to add two integers together, for example, 1
and 2
. Here is what we should expect to see.
let numOne = 1;
let numTwo = 2;
console.log(numOne + numTwo);
//Output:
3
But if our values are a string, we will get a different output.
let numOne = '1';
let numTwo = '2';
console.log(numOne + numTwo);
// Output: '12'
But what if we want to convert our string into a number? Well, luckily for us, JavaScript has a ton of built-in functions called methods
. And one of those methods is called Number()
. Next, let’s learn how to use it.
Number()
Method for converting a string to an integerThe Number()
method lets us convert the string into a number. It is a pretty handy method, but it doesn’t convert our string into an integer. So if we convert a number that isn’t whole, we will see all of the other numbers after the decimal point.
In order to use this method you just have to add the variable or type in a string into the parentheses of the method.
let string = '2.5'
let number = Number(string)
console.log(number)
// Output: 2.5
As we can see, the output isn’t an integer. What we can do is add another function to this function, which will turn our number into an integer. Well there are actually 2 methods that we can add, and the first one is:
Math.floor()
This method takes in a number and returns the largest integer, which is less than or equal to a given number. Here is how you can use this method:
let string = '5.95'
let integer = Math.floor(Number(string));
console.log(integer)
// Output: 5
Math.ceil()
This method does the opposite of Math.floor()
. It always rounds a number to the next highest integer.
let string = '5.05'
let integer = Math.ceil(Number(string));
console.log(integer)
// Output: 6
The final Math
method is:
Math.round()
This method returns a number rounded to the nearest integer.
let stringOne = '5.7'
let stringTwo = '5.1'
let integerOne = Math.round(Number(stringOne));
let integerTwo = Math.round(Number(stringTwo));
console.log(integerOne)
// Output: 6
console.log(integerTwo)
// Output: 5
I know this may seem a bit confusing and difficult at first, but these are super useful methods that everyone should know.
parseInt()
MethodNow the parseInt()
method will convert our string into an integer. It works almost the same as the Number()
method, but our output will be a whole number. Here is an example:
let string = '10.52'
let integer = parseInt(string);
console.log(integer);
// Output: 10
As you can notice, no matter what digit you put in, the output will always be the number with all of the digits after the decimal point.
It may seem that the parseInt()
method is the easiest one to use to convert a string to an integer in Javascript, but it’s always valuable to know how the other methods work and that there are multiple ways to solve a problem.
I would recommend that you play around with these methods to get the hang of them.
Hope that this helps! Regards, Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.