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!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hi all,
For everyone looking for a quick answer, here is how you could convert a string to an integer in Javascript with
parseInt()
:For anyone who wants to learn more about this, feel free to continue reading!
Introduction
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
andnumbers
. If we have a variable with the value'5'
, that is a string, but if our value is5
, 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
and2
. Here is what we should expect to see.But if our values are a string, we will get a different output.
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 calledNumber()
. Next, let’s learn how to use it.The
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.
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:
Math.ceil()
This method does the opposite of
Math.floor()
. It always rounds a number to the next highest integer.The final
Math
method is:Math.round()
This method returns a number rounded to the nearest integer.
I know this may seem a bit confusing and difficult at first, but these are super useful methods that everyone should know.
Use the
parseInt()
MethodNow the
parseInt()
method will convert our string into an integer. It works almost the same as theNumber()
method, but our output will be a whole number. Here is an example: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.
Conclusion
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