35

My guess would be:

function isNumber(val) {
    return val === +val;
}

Is there a better way?

Previous Reference

Validate decimal numbers in JavaScript - IsNumeric()

Community
  • 1
  • 1
nativist.bill.cutting
  • 1,292
  • 3
  • 11
  • 19

6 Answers6

39
var isNumber = function isNumber(value) 
{
   return typeof value === 'number' && isFinite(value);
}

This code above is taken from the book "JavaScript - the good parts".

/* ANSWER UPDATE BEGIN - in reply to some comments
below from the person who asked the question and others */

var isNumber = function isNumber(value) {
  return typeof value === 'number' && isFinite(value);
}

var isNumberObject = function isNumberObject(n) {
  return (Object.prototype.toString.apply(n) === '[object Number]');
}

var isCustomNumber = function isCustomNumber(n){
  return isNumber(n) || isNumberObject(n);
}

console.log(isCustomNumber(new Number(5)));
console.log(isCustomNumber(new Number(5.2)));
console.log(isCustomNumber(new Number(5.5)));
console.log(isCustomNumber(new Number(-1)));
console.log(isCustomNumber(new Number(-1.5)));
console.log(isCustomNumber(new Number(-0.0)));
console.log(isCustomNumber(new Number(0.0)));
console.log(isCustomNumber(new Number(0)));
console.log(isCustomNumber(new Number(1e5)));

console.log(isCustomNumber(5));
console.log(isCustomNumber(5.2));
console.log(isCustomNumber(5.5));
console.log(isCustomNumber(-1));
console.log(isCustomNumber(-1.5));
console.log(isCustomNumber(-0.0));
console.log(isCustomNumber(0.0));
console.log(isCustomNumber(0));
console.log(isCustomNumber(1e5));

/* ANSWER UPDATE END - in reply to some comments
below from the person who asked the question and others */

Martin
  • 16,093
  • 1
  • 29
  • 48
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • 1
    I like typeof but what is the isFinite for? – nativist.bill.cutting Nov 23 '13 at 23:47
  • "The isFinite function is the best way of determining whether a value can be used as a number because it rejects NaN and Infinity." This is also a quote from the same book. – peter.petrov Nov 23 '13 at 23:48
  • I see, JavaScript considers these values number because they are of type number but used to describe Not a Number. – nativist.bill.cutting Nov 23 '13 at 23:49
  • 2
    Why you use named function expression? – Givi Nov 23 '13 at 23:53
  • 1
    @Givi: most likely so that similar to native functions, the name is shown in the `toString` representation of it. – Qantas 94 Heavy Nov 23 '13 at 23:55
  • I don't particularly like this one because `isNumber(new Number(5))` would return `false`. – zzzzBov Nov 24 '13 at 00:02
  • what in the holy heck does new Number(5) do? `var test = 5 vs var test = new Number(5)` – nativist.bill.cutting Nov 24 '13 at 22:40
  • how does that created something that is not a number?? – nativist.bill.cutting Nov 24 '13 at 22:41
  • @nativist.bill.cutting, `new Number(5)` creates a `Number` object, rather than a value. It's pretty much never used, so it's not really an issue, but I prefer to allow for objects to be treated as numbers, such as `Date`, `Boolean`, and any custom objects that have `valueOf` overridden. – zzzzBov Nov 25 '13 at 00:01
  • if the language allow it, it must have some purpose? – nativist.bill.cutting Nov 25 '13 at 23:22
  • 1
    This check I quoted in my answer is as strict as possible. It is a book quote. The object new Number(5) is not a number in JS, that's obvious. So if you want to customize this check for special purposes - feel free. And my best guess of implementing that custom definition is there - see the update to my answer (this extension I think follows the philosophy of the book, and I like the book a lot, as the author really really knows what he's talking about). – peter.petrov Nov 26 '13 at 13:12
  • 2
    Just use `Number.isFinite` instead. It returns a `false` for non-`number` values. – Константин Ван Dec 30 '20 at 10:48
13

If you do not want to include strings, and you do want to include Infinity, you can compare the Number coercion of the argument to the argument:

function isNumber(n){
    return Number(n)=== n;
}

//test

[0, 1, 2, -1, 1.345e+17, Infinity, false, true, NaN, '1', '0'].map(function(itm){
    return itm+'= '+isNumber(itm);
});

// returned values
0= true
1= true
2= true
-1= true
134500000000000000= true
Infinity= true
false= false
true= false
NaN= false
'1'= false
'0'= false
kennebec
  • 102,654
  • 32
  • 106
  • 127
9
function isNumber(val){
    return typeof val==='number';
}
markasoftware
  • 12,292
  • 8
  • 41
  • 69
7

If you want "23" to be a number, then

function isNumber(val) {
    return !isNaN(val);
}
Ari Porad
  • 2,834
  • 3
  • 33
  • 51
3
function isNumber(val) {
        // negative or positive
        return /^[-]?\d+$/.test(val);
    }
davs
  • 144
  • 1
  • 2
  • 10
-4

I like this solution:

function isNumber(val) {
    return (val >=0 || val < 0);
}
Joren
  • 3,068
  • 25
  • 44