JavaScript – Converting a string into an integer?

Simplest way, use the native Number function:

var x = Number("1000")

If that doesn’t work, then there are the parseInt, unary plus, parseFloat with floor, and Math.round methods.


unary plus, if string is already in the form of an integer:

If string is or might be a float and you want an integer:

or, if you’re going to be using Math.floor several times:

If you’re the type who forgets to put the radix in when you call parseInt, you can use parseFloat and round it however you like. Here I use floor.

Interestingly, Math.round (like Math.floor) will do a string to number conversion, so if you want the number rounded (or if you have an integer in the string), this is a great way, maybe my favorite: