45

My guess would be:

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

Is there a better way?

Previous Reference

Validate decimal numbers in JavaScript - IsNumeric()

8
  • 1
    What is your expectation for the following values: NaN, Infinity, '1', 1? Commented Nov 23, 2013 at 23:32
  • 2
    You can use the implementation from jQuery for this: jQuery.isNumeric Commented Nov 23, 2013 at 23:34
  • 1
    The real numbers from math. Commented Nov 23, 2013 at 23:36
  • I just have to ask, couldn't you find an answer in the exact duplicate question you yourself posted in the question ? Commented Nov 23, 2013 at 23:36
  • 1
    What do you mean by "this would not handle negative numbers"? -1 is equal to +-1. Commented Nov 23, 2013 at 23:54

6 Answers 6

51
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 */

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

12 Comments

I like typeof but what is the isFinite for?
Why you use named function expression?
@Givi: most likely so that similar to native functions, the name is shown in the toString representation of it.
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).
Just use Number.isFinite instead. It returns a false for non-number values.
|
15

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

2 Comments

Do you consider 01 Numeric? If so this fails that case
Number(01) === 01 === true test this in a console -- it's truthy.
11
function isNumber(val){
    return typeof val==='number';
}

2 Comments

and of course && !isNaN(val) because typeof NaN is number.
@FarzadYousefzadeh TIL that 'Not a Number' is of type 'number'. Gotta love javascript.
8

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

function isNumber(val) {
    return !isNaN(val);
}

2 Comments

above function will given wrong with null and empty string , use follwing for right one function isNumber(n) { return !n && isFinite(n) }
it return true when val is empty string. but nice and simple answer.
2
function isNumber(val) {
        // negative or positive
        return /^[-]?\d+$/.test(val);
    }

3 Comments

1.1 will fail, is that intentional?
...can you break Joren's answser?
I think his solution is ok
-4

I like this solution:

function isNumber(val) {
    return (val >=0 || val < 0);
}

3 Comments

detects true an false.
Well since true and false can be seen as 1 or 0 I guess that it is a desired functionality.
"can be seen as" and "is" are two very different things. What happens when your function returns true for a boolean, and so you try to invoke a Number.prototype method on the val? Oops!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.