views:

4219

answers:

6

I'm hoping there's something in the same conceptual space as the old VB6 IsNumeric() function?

+11  A: 

Try the isNan function...

http://www.w3schools.com/jsref/jsref_isNaN.asp

theraccoonbear
Spectacular! Exactly what I was looking for.
Electrons_Ahoy
Glad I could help :)
theraccoonbear
Make sure you add a check for the empty string. isNaN('') returns false but you probably want it to return true in this case.
Michael Haren
+2  A: 

(pasrseInt(value) === value)

Russ
+2  A: 

parseInt(), but be aware that this function is a bit different in the sense that it for example returns 100 for parseInt("100px").

liggett78
+1  A: 

See this related question, which I asked some time ago.

Michael Haren
If you go to this question, try to skip past all the RegEx answers. That's just NOT the way to do it.
Joel Coehoorn
+1  A: 

And you could go the RegExp-way:

var num = "987238";

if(num.match(/^\d+$/)){
  //valid integer
}else if(num.match(/^\d+\.\d+$/)){
  //valid float
}else{
  //not valid number
}
roenving
In this case, RegExp == bad
Joel Coehoorn
+41  A: 

To check to see if a variable is not a number:

This works regardless of whether the variable contains is a string or number.

isNaN(num)     // returns true if the variable does NOT contain a valid number

Examples:

isNaN(123)     // false
isNaN('123')   // false
isNaN('foo')   // true
isNaN('10px')  // true

Of course, you can negate this if you need to. For example, to implement the IsNumeric example you gave:

function isNumeric(num){
    return !isNaN(num)
}

To convert a string containing a number into a number:

only works if the string only contains numeric characters, else it returns NaN.

+num              // returns the numeric value of the string, or NaN if the 
                  // string isn't purely numeric characters

Examples:

+'12'             // 12
+'foo'            // NaN
+'12px'           // NaN

To convert a string loosely to a number

useful for converting '12px' to 12, for example.

parseInt(num)     // extracts a numeric value from the 
                  // start of the string, or NaN.

Examples:

parseInt('12')    // 12
parseInt('aaa')   // NaN
parseInt('12px')  // 12
parseInt('foo2')  // NaN      These last two may be different
parseInt('12a5')  // 12       from what you expected to see.

Floats

Bear in mind that, unlike +num, parseInt (as the name suggests) will convert a float into an integer by chopping off everything following the decimal point (if you want to use parseInt() because of this behaviour, you're probably better off with Math.floor() instead):

parseInt(12.345)   // 12
parseInt('12.345') // 12
+'12.345'          // 12.345

Empty strings

Empty strings may be a little counter-intuitive. +num converts empty strings to zero, and isNaN() assumes the same:

+''                // 0
isNaN('')          // false

But parseInt() does not agree:

parseInt('')       // NaN
Dan
Great post! upvote
Joel Coehoorn
OOh, yes! Perfect. You've nicely answered all the follow-up questions I just had forming in my brain. Thanks.
Electrons_Ahoy
This is a great model of a perfect post - well organized and meaningful.
Erik Forbes
A very important note about parseInt is that it will allow you to specify a radix for converting the string to an int. This is a big gotcha as it tries to guess a radix for you if you don't supply it. So, for example: parseInt("17") results in 17 (decimal, 10), but parseInt("08") results in 0 (octal, 8).So, unless you intend otherwise, it is safest to use parseInt(number, 10), specifying 10 as the radix explicitly.
Adam Raney