0

I have following array

var arr = [7, 1, "abc", undefined, NaN];

but how can i check if the value in arr is a NaN ?

for (i=0; i < arr.length; i++){

    // check if array value is false or NaN
    if (arr[i] === false || arr[i] === NaN) {
        // do something
    }

  }

any idea ? I tried isNaN(arr[i]) function but it is also not working.

Muflix
  • 6,192
  • 17
  • 77
  • 153
  • 1
    NaN is the only value in JS which is not equal to anything else, even itself. So NaN === NaN always returns false. –  Sep 09 '16 at 10:22
  • Try `Number.isNaN` – Bergi Sep 09 '16 at 10:22
  • Use function isNaN(NaN), or isFinite(NaN) to obtain true / false if value is valid / not valid number. –  Sep 09 '16 at 10:31

5 Answers5

4

For me this is working??

for (i=0; i < arr.length; i++){

    // check if array value is false or NaN
    if (arr[i] === false || Number.isNaN(arr[i]) ) {
        console.log("NaN");
    }

  }
Hassan
  • 930
  • 3
  • 16
  • 35
  • same here : http://codepen.io/anon/pen/VKvYOO – Benjamin Sep 09 '16 at 10:17
  • 2
    This is returning all items that aren't a number, not all instances of NaN. My interpretation of the question is that 'abc' and undefined are OK and we just want to find the NaN number - see my answer below for this – smuff Sep 09 '16 at 10:25
  • @smuff Thanks for pointing this out :) – Hassan Sep 09 '16 at 10:44
3

You could check the type (typeof NaN === 'number') and with isNaN for a NaN value.

var arr = [7, 1, "abc", undefined, NaN], i;
for (i = 0; i < arr.length; i++){
    if (typeof arr[i] === 'number' && isNaN(arr[i])) {
        console.log('Element ' + i + ' is NaN');
    }
}

Or check if the value is unequal to itself.

var arr = [7, 1, "abc", undefined, NaN], i;
for (i = 0; i < arr.length; i++){
    if (arr[i] !== arr[i]) {
        console.log('Element ' + i + ' is NaN');
    }
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

First convert the value toString. Then check value == 'NaN'

for (i=0; i < arr.length; i++){

    // check if array value is false or NaN
    if (arr[i] === false || (arr[i] != undefined && typeof(arr[i]) == "number" && arr[i].toString() === 'NaN')) {
        // do something
    }

  }
intekhab
  • 1,566
  • 1
  • 13
  • 19
  • 1
    This will run for the string 'NaN' also, rather than just the actual value NaN. It would fire twice for the array [NaN, 'NaN'] – smuff Sep 09 '16 at 10:46
  • We can check first `typeof` `NaN`. If `typeof(NaN) == "number"` then proceed, see my update. Well this is not a good idea. `Accepted` answer is a good idea – intekhab Sep 09 '16 at 11:43
1

In ECMAScript 6 you can use the Number.isNaN function:

var arr = [7, 1, "abc", undefined, NaN];

for (i=0; i < arr.length; i++){

    // check if array value is false or NaN
    if (arr[i] === false || Number.isNaN(arr[i])) {
        console.log("NaN found at place " + i);
    }

  }

If you need to work in ECMAScript 5 then the problem with the isNaN function is that it is checking to see if the value is able to be coerced into a number. Instead you could use the odd property of the NaN value - it isn't equal to itself:

var arr = [7, 1, "abc", undefined, NaN];

for (i=0; i < arr.length; i++){

    // check if array value is false or NaN
    if (arr[i] === false || arr[i] != arr[i]) {
        console.log("NaN found at place " + i);
    }

  }
smuff
  • 452
  • 6
  • 14
1

isNan works for me:

for (i=0; i < arr.length; i++){
    // check if array value is false or NaN
    if (isNaN(arr[i])) {
        console.log("Not a number at index " + i + ": "+arr[i]);
    }
}

prints (in node.js):

Not a number at index 2: abc
Not a number at index 3: undefined
Not a number at index 4: NaN
ErkkiRuohtula
  • 133
  • 1
  • 4