How to check if a JavaScript number is a real, valid number?
If you want to check whether a number is a real number, you should also check whether it's finite:
function isNumber(n){
return typeof n == 'number' && !isNaN(n) && isFinite(n);
}
Another method (explanation below):
function isNumber(n){
return typeof n == 'number' && !isNaN(n - n);
}
Update: Two expressions to validate a real number
Since JavaScript numbers are representing real numbers, the substraction operand on the same number should produce the zero value (additive identity). Numbers out of range should (and will) be invalid, NaN
.
1 - 1 = 0 // OK
Infinity - Infinity = NaN // Expected
NaN - NaN = NaN // Expected
NaN - Infinity = NaN
JS numbers can be among the following values:
- Finite numbers
+Infinity
and-Infinity
NaN
Then there also non-number values which are coercible to numbers, e.g. number objects. You might want to consider them numerical.
If you only want to test finite numbers, simply use Number.isFinite
:
Number.isFinite(value)
var isNumber = Number.isFinite;
assert('isNumber(1)', true);
assert('isNumber(1.1)', true);
assert('isNumber(+0)', true);
assert('isNumber(-0)', true);
assert('isNumber(-1.1)', true);
assert('isNumber(Math.PI)', true);
assert('isNumber(1e300)', true);
assert('isNumber(+Infinity)', false);
assert('isNumber(-Infinity)', false);
assert('isNumber(NaN)', false);
assert('isNumber(null)', false);
assert('isNumber(undefined)', false);
assert('isNumber(true)', false);
assert('isNumber(false)', false);
assert('isNumber("123")', false);
assert('isNumber("foo")', false);
assert('isNumber(new Number(1))', false);
assert('isNumber([])', false);
assert('isNumber({})', false);
assert('isNumber(function(){})', false);
function assert(code, expected) {
var result = eval(code);
console.log('Test ' + (result===expected ? 'pass' : 'FAIL') + ': ', code, ' -> ', result);
}
If you want to include infinities, check the type and exclude NaN
:
typeof value === "number" && !Number.isNaN(value)
function isNumber(value) {
return typeof value === "number" && !Number.isNaN(value);
}
assert('isNumber(1)', true);
assert('isNumber(1.1)', true);
assert('isNumber(+0)', true);
assert('isNumber(-0)', true);
assert('isNumber(-1.1)', true);
assert('isNumber(Math.PI)', true);
assert('isNumber(1e300)', true);
assert('isNumber(+Infinity)', true);
assert('isNumber(-Infinity)', true);
assert('isNumber(NaN)', false);
assert('isNumber(null)', false);
assert('isNumber(undefined)', false);
assert('isNumber(true)', false);
assert('isNumber(false)', false);
assert('isNumber("123")', false);
assert('isNumber("foo")', false);
assert('isNumber(new Number(1))', false);
assert('isNumber([])', false);
assert('isNumber({})', false);
assert('isNumber(function(){})', false);
function assert(code, expected) {
var result = eval(code);
console.log('Test ' + (result===expected ? 'pass' : 'FAIL') + ': ', code, ' -> ', result);
}
If you want to consider number objects as numbers, you can unwrap them using
value = Number.valueOf.call(value); // throws if value was not a number object
function isNumber(value) {
try { value = Number.prototype.valueOf.call(value); } catch(err) { }
return Number.isFinite(value);
}
assert('isNumber(1)', true);
assert('isNumber(1.1)', true);
assert('isNumber(+0)', true);
assert('isNumber(-0)', true);
assert('isNumber(-1.1)', true);
assert('isNumber(Math.PI)', true);
assert('isNumber(1e300)', true);
assert('isNumber(+Infinity)', false);
assert('isNumber(-Infinity)', false);
assert('isNumber(NaN)', false);
assert('isNumber(null)', false);
assert('isNumber(undefined)', false);
assert('isNumber(true)', false);
assert('isNumber(false)', false);
assert('isNumber("123")', false);
assert('isNumber("foo")', false);
assert('isNumber(new Number(1))', true);
assert('isNumber([])', false);
assert('isNumber({})', false);
assert('isNumber(function(){})', false);
function assert(code, expected) {
var result = eval(code);
console.log('Test ' + (result===expected ? 'pass' : 'FAIL') + ': ', code, ' -> ', result);
}
If you want to include arbitrary values coercible to numbers, you can use the unary +
to coerce.
value = +value; // throws if value was not number-coercible
There is also the isNaN
function (not to be confused with Number.isNaN
), which will first coerce and then compare with NaN
. But be aware whitespace strings and null
are coerced to +0
, not NaN
. So you might be interested in Validate decimal numbers in JavaScript - IsNumeric()