Site Navigation

Wednesday, June 11, 2008

bug 417 - isNaN() might return false when you expected true

Issue: #417
Affects: All Browsers

The JavaScript isNaN(testValue) function will return true if the testValue is Not A Number. Likewise a false return value would signify that the testValue was actually a number.

isNaN('foo');//true
isNaN(37);//false
isNaN('123456789w');//true
isNaN('12345e6789');//false

Huh?! What?! but there is a letter in there!

Since Scientific notation allows the letter 'e' to be used to indicate very, very small and very, very big numbers ("e" represents "times ten raised to the power"), one can only guess that the parsing allows all digits, zero or one decimal, AND zero or one 'e' character...

Tuck that one in your JavaScript toolbelt for a rainy day. ;-)


Known Workarounds: One. If this truly worries you when reading in a value, you can run an additional test against the string...

Example Workaround Code:

var testStr = '12345e6789';
var isNumber = !isNaN(testStr);
if(isNumber){
if(testStr.indexOf('e') != -1){
isNumber = false;
}
}
alert('The test value is a number?: ' + isNumber);



Keep in mind, that '123e14' is a valid number, just likely not what your application is expecting in terms of the size of the number. ;-)

Related Issues: None.

Bug/Site Feedback |
Submit a bug