views:

1508

answers:

11

Hi, all.

I have a quick question (I hope!). In JS, why does isNaN(" ") evaluate to false, but isNaN(" x") evaluate to true?

I'm performing numerical operations on a text input field, and am checking if the field is null, "", or NaN. When someone types a handful of spaces into the field, my validation fails on all three, and I'm confused as to why it gets past the isNAN check.

Thanks!

+11  A: 

JavaScript interprets an empty string as a 0, which then fails the isNAN test. You can use parseInt on the string first which won't convert the empty string to 0. The result should then fail isNAN.

Antonio Haley
+4  A: 

Try using:

alert(isNaN(parseInt("   ")));

Or

alert(isNaN(parseFloat("    ")));
bendewey
+2  A: 

I think it's because of Javascript's typing: ' ' is converted to zero, whereas 'x' isn't:

alert(' ' * 1); // 0
alert('x' * 1); // NaN
Greg
A: 

isNaN will always return false for a string of nothing but spaces. I would suggest doing string trimming before evaluation.

Scott Lance
+2  A: 

I'm not sure why, but to get around the problem you could always trim whitespace before checking. You probably want to do that anyway.

Joel Coehoorn
+1  A: 

If you look at http://www.w3schools.com/jsref/jsref_isNaN.asp you will see that strings evaluate to true. I believe that is because the characters in the string can be converted to numbers.

isNaN is used to test numbers.

James Black
+5  A: 

To understand it better, please open Ecma-Script spec pdf on page 43 "ToNumber Applied to the String Type"

if a string has a numerical syntax, which can contain any number of white-space characters, it can be converted to Number type. Empty string evaluates to 0. Also the string 'Infinity' should give

isNaN('Infinity'); // false
Rafael
+2  A: 

Here's a question which answers this:

http://stackoverflow.com/questions/115548/why-is-isnannull-false-in-js

Lucero
+3  A: 

You may find this surprising or maybe not, but here is some test code to show you the wackyness of the JavaScript engine.

document.write(isNaN("")) // false
document.write(isNaN(" "))  // false
document.write(isNaN(0))  // false
document.write(isNaN(null)) // false
document.write(isNaN(false))  // false
document.write("" == false)  // true
document.write("" == 0)  // true
document.write(" " == 0)  // true
document.write(" " == false)  // true
document.write(" " == 0)  // true
document.write(0 == false) // true
document.write(" " == "") // false

so this means that

" " == 0 == false

and

"" == 0 == false

but

"" != " "

Have fun :)

Nick Berardi
+1 Great post. Can you add how the triple equals (=== and !==) operator fits here?
bendewey
You should try NaN===NaN or NaN==NaN;-) I don't know if all this means the javascript engine is wacky or that javascript is bad for wacky programmers though.
KooiInc
+1  A: 

If you would like to implement an accurate isNumber function, here is one way to do it from Javascript: The Good Parts by Douglas Crockford [page 105]

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

I suggest you to use the following function if you really want a proper check if it is an integer:

function isInteger(s)
{
   return Math.ceil(s) == Math.floor(s);
}
Deepesh