Try these functions to test whether a value is a number primitive value that has no fractional part and is within the size limits of what can be represented as an exact integer.
function isFloat(n) {
return n === +n && n !== (n|0);
}
function isInteger(n) {
return n === +n && n === (n|0);
}
It really doesn't have to be so complicated. The numeric value of an integer's parseFloat() and parseInt() equivalents will be the same. Thus you can do like so:
function isInt(value){
return (parseFloat(value) == parseInt(value)) && !isNaN(value);
}
Then
if (isInt(x)) // do work
This will also allow for string checks and thus is not strict. If want a strong type solution (aka, wont work with strings):
function is_int(value){ return !isNaN(parseInt(value * 1) }
I like this little function, which will return true for both positive and negative integers:
function isInt(val) {
return ["string","number"].indexOf(typeof(val)) > -1 && val !== '' && !isNaN(val+".0");
}
This works because 1 or "1" becomes "1.0", which isNaN() returns false on (which we then negate and return), but 1.0 or "1.0" becomes "1.0.0", while "string" becomes "string.0", neither of which are numbers, so isNaN() returns false (and, again, gets negated).
If you only want positive integers, there's this variant:
function isPositiveInt(val) {
return ["string","number"].indexOf(typeof(val)) > -1 && val !== '' && !isNaN("0"+val);
}
or, for negative integers:
function isNegativeInt(val) {
return `["string","number"].indexOf(typeof(val)) > -1` && val !== '' && isNaN("0"+val);
}
isPositiveInt() works by moving the concatenated numeric string ahead of the value to be tested. For example, isPositiveInt(1) results in isNaN() evaluating "01", which evaluates false. Meanwhile, isPositiveInt(-1) results in isNaN() evaluating "0-1", which evaluates true. We negate the return value and that gives us what we want. isNegativeInt() works similarly, but without negating the return value of isNaN().
Edit:
My original implementation would also return true on arrays and empty strings. This implementation doe not have that defect. It also has the benefit of returning early if val is not a string or number, or if it's an empty string, making it faster in these cases. You can further modify it by replacing the first two clauses with
typeof(val) != "number"
if you only want to match literal numbers (and not strings)
Edit:
I can't post comments yet, so I'm adding this to my answer. The benchmark posted by @Asok is very informative; however, the fastest function does not fit the requirements, as it also returns TRUE for floats, arrays, booleans, and empty strings.
I created the following test suite to test each of the functions, adding my answer to the list, as well (function 8, which parses strings, and function 9, which does not):
funcs = [
function(n) {
return n % 1 == 0;
},
function(n) {
return typeof n === 'number' && n % 1 == 0;
},
function(n) {
return typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n);
},
function(n) {
return n.toString().indexOf('.') === -1;
},
function(n) {
return n === +n && n === (n|0);
},
function(n) {
return parseInt(n) === n;
},
function(n) {
return /^-?[0-9]+$/.test(n.toString());
},
function(n) {
if ((undefined === n) || (null === n)) {
return false;
}
if (typeof n == 'number') {
return true;
}
return !isNaN(n - 0);
},
function(n) {
return ["string","number"].indexOf(typeof(n)) > -1 && n !== '' && !isNaN(n+".0");
}
];
vals = [
[1,true],
[-1,true],
[1.1,false],
[-1.1,false],
[[],false],
[{},false],
[true,false],
[false,false],
[null,false],
["",false],
["a",false],
["1",null],
["-1",null],
["1.1",null],
["-1.1",null]
];
for (var i in funcs) {
var pass = true;
console.log("Testing function "+i);
for (var ii in vals) {
var n = vals[ii][0];
var ns;
if (n === null) {
ns = n+"";
} else {
switch (typeof(n)) {
case "string":
ns = "'" + n + "'";
break;
case "object":
ns = Object.prototype.toString.call(n);
break;
default:
ns = n;
}
ns = "("+typeof(n)+") "+ns;
}
var x = vals[ii][1];
var xs;
if (x === null) {
xs = "(ANY)";
} else {
switch (typeof(x)) {
case "string":
xs = "'" + n + "'";
break;
case "object":
xs = Object.prototype.toString.call(x);
break;
default:
xs = x;
}
xs = "("+typeof(x)+") "+xs;
}
var rms;
try {
var r = funcs[i](n);
var rs;
if (r === null) {
rs = r+"";
} else {
switch (typeof(r)) {
case "string":
rs = "'" + r + "'";
break;
case "object":
rs = Object.prototype.toString.call(r);
break;
default:
rs = r;
}
rs = "("+typeof(r)+") "+rs;
}
var m;
var ms;
if (x === null) {
m = true;
ms = "N/A";
} else if (typeof(x) == 'object') {
m = (xs === rs);
ms = m;
} else {
m = (x === r);
ms = m;
}
if (!m) {
pass = false;
}
rms = "Result: "+rs+", Match: "+ms;
} catch (e) {
rms = "Test skipped; function threw exception!"
}
console.log(" Value: "+ns+", Expect: "+xs+", "+rms);
}
console.log(pass ? "PASS!" : "FAIL!");
}
I also reran the benchmark with function #8 added to the list. I won't post the result, as they're a bit embarrassing (e.g. that function is NOT fast)...
The (abridged -- I removed successful tests, since the output is quite long) results are as follows:
I've left in failures so you can see where each function is failing, and the (string) '#' tests so you can see how each function handles integer and float values in strings, as some may want these parsed as numbers and some may not.
Out of the 10 functions tested, the ones that actually fit OP's requirements are [1,3,5,6,8,9]
Some times Number objects don't allow you to use direct the mod operator (%), if you are facing that case you can use this solution.
You can use a simple regular expression:
Or you can use the below functions too, according your needs. They are developed by the PHPJS Project.
is_int()
=> Check if variable type is integer and if its content is integeris_float()
=> Check if variable type is float and if its content is floatctype_digit()
=> Check if variable type is string and if its content has only decimal digitsUpdate 1
Now it checks negative numbers too, thanks for @ChrisBartley comment!
I wrote function which accepts strings (if somebody will need)
example ouptuts:
Try these functions to test whether a value is a number primitive value that has no fractional part and is within the size limits of what can be represented as an exact integer.
It really doesn't have to be so complicated. The numeric value of an integer's parseFloat() and parseInt() equivalents will be the same. Thus you can do like so:
Then
This will also allow for string checks and thus is not strict. If want a strong type solution (aka, wont work with strings):
I like this little function, which will return true for both positive and negative integers:
This works because 1 or "1" becomes "1.0", which isNaN() returns false on (which we then negate and return), but 1.0 or "1.0" becomes "1.0.0", while "string" becomes "string.0", neither of which are numbers, so isNaN() returns false (and, again, gets negated).
If you only want positive integers, there's this variant:
or, for negative integers:
isPositiveInt() works by moving the concatenated numeric string ahead of the value to be tested. For example, isPositiveInt(1) results in isNaN() evaluating "01", which evaluates false. Meanwhile, isPositiveInt(-1) results in isNaN() evaluating "0-1", which evaluates true. We negate the return value and that gives us what we want. isNegativeInt() works similarly, but without negating the return value of isNaN().
Edit:
My original implementation would also return true on arrays and empty strings. This implementation doe not have that defect. It also has the benefit of returning early if val is not a string or number, or if it's an empty string, making it faster in these cases. You can further modify it by replacing the first two clauses with
if you only want to match literal numbers (and not strings)
Edit:
I can't post comments yet, so I'm adding this to my answer. The benchmark posted by @Asok is very informative; however, the fastest function does not fit the requirements, as it also returns TRUE for floats, arrays, booleans, and empty strings.
I created the following test suite to test each of the functions, adding my answer to the list, as well (function 8, which parses strings, and function 9, which does not):
I also reran the benchmark with function #8 added to the list. I won't post the result, as they're a bit embarrassing (e.g. that function is NOT fast)...
The (abridged -- I removed successful tests, since the output is quite long) results are as follows:
I've left in failures so you can see where each function is failing, and the (string) '#' tests so you can see how each function handles integer and float values in strings, as some may want these parsed as numbers and some may not.
Out of the 10 functions tested, the ones that actually fit OP's requirements are [1,3,5,6,8,9]