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?
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?
The javascript function isNaN(variable) should do the trick. It returns true if the data is not a number.
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.'infinity'; better use isFinite(String(foo))isNaN(null) returns false.I'd go with
isFinite(String(foo)) See this answer for an explanation why. If you only want to accept integer values, look here.
isFinite(null) === trueisFinite(String(" ")) == isFinite(String("\t")) == isFinite(String("")) == true. See the answer linked.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?!
typeof(something) == 'number'. With == rather than ===. Reserve === for when you really mean it.=== 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.pdfYou 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()
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.
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 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"