40

How can I check that a variable is a number, either an integer or a string digit?

In PHP I could do:

if (is_int($var)) { echo '$var is integer'; } 

Or:

if (is_numeric($var)) { echo '$var is numeric'; } 

How can I do it in jQuery/JavaScript?

2
  • I'm assuming that you are trying to avoid a round-trip to your server to test? That would be the brain-dead-easy way ;) Commented Aug 29, 2009 at 22:51
  • Note to future readers: This question asks one question in the body (check if a variable is either an integer or a string of digits) and a slightly different question in the title (check if a variable is a number). Which question the answers address varies, and none of the top answers correctly answer either question (except, arguably, this one, if you don't want numeric strings and NaN counts as a number). You should refer instead to the duplicate target. Commented Aug 13, 2024 at 10:31

10 Answers 10

44

The javascript function isNaN(variable) should do the trick. It returns true if the data is not a number.

Sign up to request clarification or add additional context in comments.

4 Comments

This is a good suggestion. FWIW, this works because strings are implicitly converted to numbers - the explicit code is: isNaN(new Number(variable)). Note that if you wish to restrict the code to integers, parseInt() will return NaN (testable via isNaN()). Be aware though that parseInt() will happily ignore non-numberic digits at the end of the string.
this will accept everything which can be converted to a number, including boolean values and 'infinity'; better use isFinite(String(foo))
Don't use isNaN, it's broken. See developer.mozilla.org/en-US/docs/JavaScript/Reference/… and use the solution proposed by @Christoph instead.
On Node, isNaN(null) returns false.
28

I'd go with

isFinite(String(foo)) 

See this answer for an explanation why. If you only want to accept integer values, look here.

6 Comments

Watch out for this. isFinite(null) === true
isFinite("100") is true, we want to know that an object is a Number object. From my brief testing it looks like the solution here (Object.prototype.toString.call(obj) === "[object Number]") is the best: stackoverflow.com/questions/1303646/…
Also returns true with an empty string, which I wouldn't consider a "number"
Object.prototype.toString.call(obj) === "[object Number]" will return true if passing NaN
Does not handle all variables: isFinite(String(" ")) == isFinite(String("\t")) == isFinite(String("")) == true. See the answer linked.
|
14

I'm pretty new to javascript but it seems like typeof(blah) lets you check if something is a number (it doesn't say true for strings). A know the OP asked for strings + numbers but I thought this might be worth documenting for other people.

eg:

function isNumeric(something){ return typeof(something) === 'number'; } 

Here are the docs

and here's a few console runs of what typeof produces:

typeof(12); "number" typeof(null); "object" typeof('12'); "string" typeof(12.3225); "number" 

one slight weirdness I am aware of is

typeof(NaN); "number" 

but it wouldn't be javascript without something like that right?!

2 Comments

It's better JS style to use typeof(something) == 'number'. With == rather than ===. Reserve === for when you really mean it.
@AdamChalcraft most linters/programmers would probably disagree and say the opposite - always use === unless there's a good reason not to, if you'll get into the habit of using == you'll get burned by something bizaare happening at some point. In this case there's no type coercion going on so it's a fairly moot point either way. For an educational (and also hilarious) rant on the topic check out James Mickens' "To wash it all away" scholar.harvard.edu/files/mickens/files/towashitallaway.pdf
6

You should use:

if(Number.isInteger(variable)) alert("It is an integer"); else alert("It is not a integer"); 

you can find the reference in: Number.isInteger()

Comments

1
function isNumeric( $probe ) { return parseFloat( String( $probe ) ) == $probe; } 

Comments

1

You should use:

var x = 23; var y = 34hhj; isNaN(x); isNaN(y); 

It will return true if its string and false if its a number.

1 Comment

Duplicate of stackoverflow.com/a/1352579 (which is also wrong), just with syntactically invalid sample code and an incorrect explanation.
0

You should use

simple way to check whether given value is number by using "if condition"

function isInteger(value) { num=value.trim(); return !(value.match(/\s/g)||num==""||isNaN(num)||(typeof(value)=='number'); } 

it will return true if the value which we are passing is an integer..

solved for value="" //false null value="12" //true only integers value="a" //false value=" 12" //false value="12 " //false value=" " //false space value="$12" //false special characters value="as12" //false 

Comments

0

Be aware that empty string '' and ' ' will be considered as number by isNaN and isFinite.

Comments

0

You should use: $.isNumeric( i )

jQuery.isNumeric API

Comments

-1

The typeof operator returns a string indicating the type of the unevaluated operand.

const num = 42; console.log(typeof num === "number"); // expected return true if(typeof num === "number"){ console.log("This is number") }

More on it......

console.log(typeof 42); // expected output: "number" console.log(typeof 'blubber'); // expected output: "string" console.log(typeof true); // expected output: "boolean" console.log(typeof undeclaredVariable); // expected output: "undefined"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.