Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  76] [ 4]  / answers: 1 / hits: 22291  / 13 Years ago, thu, january 19, 2012, 12:00:00

I am using isNan functionto check if entered value is a number:

if(isNaN(num)) {
alert('I am not a number');
else {

However, if entered value is, for example, 5748hh it is understandable like a number and return false. What is wrong?

More From » jquery


from Mozilla MDN

When the argument to the isNaN function is not a number, the value is first coerced to a number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the false returned value may be unexpected; the empty string, for example, is surely not a number. The confusion stems from the fact that the term, not a number, has a specific meaning for numbers represented as IEEE-794 floating-point values. The function should be interpreted as answering the question, is this value, when coerced to a numeric value, an IEEE-794 'Not A Number' value?

so alert(isNaN('45345ll')) returns true because its value coercion is different than a parseInt/parseFloat conversion.


var num = false;
console.log(isNaN(num)); /* return false because the coerced value is 0 but */
console.log(parseInt(num, 10)); /* return isNaN */

Maybe you get an unexpected value due to a missing paren in your if statement

[#87922] Wednesday, January 18, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 671
Total Questions: 100
Total Answers: 102

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
rhett questions
Thu, Oct 28, 21, 00:00, 3 Years ago
Mon, Aug 2, 21, 00:00, 3 Years ago
Fri, Dec 11, 20, 00:00, 3 Years ago
Fri, Aug 7, 20, 00:00, 4 Years ago
Thu, Mar 26, 20, 00:00, 4 Years ago