Andrius Andrius - 4 months ago 6x
Javascript Question

Javascript - check if string can be converted to integer?

How can I simply check if string can be converted to integer?

And I mean only integer, so string like

would not be converted to

For example if I do:

parseInt('10.1', 10)

It returns 10

Is there something equivalent like it is in Python, so I would not need to do lots of checks?

In Python if I do
, it gives me error, but if string is actually integer, only then it lets you convert it like
. So it is very easy to check that without even using regex or some additional checks.

I tried this:

function isInteger (value) {
if ($.isNumeric(value) && Number(value) % 1 === 0){
return true;
} else {
return false;

It kinda works, but if I write for example
, it will return true, because
converts to
. I guess I need to use regex to check such thing?

P.S. I searched for this question, but all answers seem to be vague and actually does not answer how to properly check for integer (not just if it is number).


One possibility is to use a regex to test, like so:

function isInteger(value) {
  return /^\d+$/.test(value);