Asked  7 Months ago    Answers:  5   Viewed   32 times

I’ve only been trying it in Firefox’s JavaScript console, but neither of the following statements return true:

parseFloat('geoff') == NaN;

parseFloat('geoff') == Number.NaN;

 Answers

30

Try this code:

isNaN(parseFloat("geoff"))

For checking whether any value is NaN, instead of just numbers, see here: How do you test for NaN in Javascript?

Tuesday, June 1, 2021
 
innovation
answered 7 Months ago
43

This worked for me:

$get("isAgeSelected ").checked == true

Where isAgeSelected is the id of the control.

Also, @karim79's answer works fine. I am not sure what I missed at the time I tested it.

Note, this is answer uses Microsoft Ajax, not jQuery

Tuesday, June 1, 2021
 
julesj
answered 7 Months ago
14

check for a remainder when dividing by 1:

function isInt(n) {
   return n % 1 === 0;
}

If you don't know that the argument is a number you need two tests:

function isInt(n){
    return Number(n) === n && n % 1 === 0;
}

function isFloat(n){
    return Number(n) === n && n % 1 !== 0;
}

Update 2019 5 years after this answer was written, a solution was standardized in ECMA Script 2015. That solution is covered in this answer.

Tuesday, June 1, 2021
 
talkhabi
answered 7 Months ago
80

NaN is not equal to anything, not even NaN.

Reference at MDN

More detailed SO question and answer

For the authoritative source, see the ECMAScript 5 Official Specification, sections 11.9.1 and 11.9.3:

1. If Type(x) is the same as Type(y), then
     [...]
  c. If Type(x) is Number, then
     i. If x is NaN, return false.
    ii. If y is NaN, return false.
        [...]
Saturday, August 7, 2021
 
mariaoialvarez
answered 4 Months ago
23

NaN is a primitive Number value. Just like 1, 2, etc.

Saturday, November 6, 2021
 
Elio Campitelli
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share