15
[1, 2, 3].indexOf(3) => 2

[1, 2, NaN].indexOf(NaN) => -1

[1, NaN, 3].indexOf(NaN) => -1
AL-zami
  • 8,902
  • 15
  • 71
  • 130
mcandre
  • 22,868
  • 20
  • 88
  • 147
  • 1
    You can use [`Array.prototype.includes`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) if you just want to know **if an array includes a `NaN`**: `[NaN].includes(NaN) /* true */`. – Константин Ван Oct 02 '16 at 01:43

3 Answers3

15

You can use Array.prototype.findIndex method to find out the index of NaN in an array

let index = [1,3,4,'hello',NaN,3].findIndex(Number.isNaN)
console.log(index)

You can use Array.prototype.includes to check if NaN is present in an array or not. It won't give you the index though !! It will return a boolean value. If NaN is present true will be returned, otherwise false will be returned

let isNaNPresent = [1,2,NaN,'ball'].includes(NaN)
console.log(isNaNPresent)

Don't use Array.prototype.indexOf

You can not use Array.Prototype.indexOf to find index of NaN inside an array.Because indexOf uses strict-equality-operator internally and NaN === NaN evaluates to false.So indexOf won't be able to detect NaN inside an array

[1,NaN,2].indexOf(NaN) // -1

Use Number.isNaN instead of isNaN :

Here i choose Number.isNaN over isNaN. Because isNaN treats string literal as NaN.On the other hand Number.isNaN treats only NaN literal as NaN

isNaN('hello world') // true
Number.isNaN('hello world') // false

Or, Write your own logic :

You can write your own logic to find NaN.As you already know that, NaN is the only value in javascript which is not equal to itself. That's the reason i suggested not to use Array.prototype.indexOf.

NaN === NaN // false

We can use this idea to write our own isNaN function.

[1,2,'str',NaN,5].findIndex(e=>e!=e) // 3
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • Would it not be clearer here to just pass in the isNaN function? Also `findIndex` is available in more implementations than arrow functions, so all it's doing is reducing the portability of this answer. – Will S Mar 21 '18 at 12:15
  • 1
    @WillS i have modified my answer – AL-zami Jul 03 '18 at 10:44
13

NaN is defined not to be equal to anything (not even itself). See here: http://www.w3schools.com/jsref/jsref_isNaN.asp

phooji
  • 10,086
  • 2
  • 38
  • 45
  • 1
    Will upvote and accept when my timer runs out. That's just retarded. – mcandre Mar 14 '11 at 03:35
  • Explain that to the IEEE; it's their floating point standard that defines `NaN` to not be equal to anything. (That said, think of it as `NULL` in RDBMSes.) – geekosaur Mar 14 '11 at 03:37
  • 1
    -1 is also used as an error value. Would it make any sense for -1 != -1 ? – mcandre Mar 14 '11 at 03:42
  • 4
    @mcandre, it's _not_ retarded. The whole point of NaN is to represent things that are considered not representable in the domain. That's not _one_ type of thing, it's many. For example, you cannot possibly say that the square root of -1, `i`, is the same as zero divided by itself (indeterminate). Yet they are both NaNs. – paxdiablo Mar 14 '11 at 03:49
  • I get that. I do. But from a practical standpoint, if I want to, say, extract all the NaNs from an array, it would be useful to compare elements to NaN rather than having to call isNaN(). – mcandre Mar 14 '11 at 18:03
2

You have to look at each item to return an array of the indexes that are NaN values-

function findNaNs(arr){
    return arr.map(function(itm, i){
        if(isNaN(itm)) return i;
        return false;
    }).filter(function(itm){
        return itm;
    });
}

findNaNs([1, NaN, 3, 4, 'cat'/3])

//or to find the first one-

function firstNaN(arr){
    var i= 0, L= arr.length;
    while(i<L){
        if(isNaN(arr[i])) return i;
        ++i;
    }
    return -1;
}
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • Thank you, kennebec. It's not hard to code around this problem, just inconvenient. – mcandre Mar 14 '11 at 18:04
  • 1
    Never use `isNaN`, it's broken. It implicitly coerces value type. You get `true` for `isNaN(undefined)`, `isNaN({})` (but not `isNaN([])`), etc. Use comparison to itself instead: `x !== x` (`NaN` is the only value that doesn't equal self). – Nelo Mitranim Oct 29 '15 at 13:38
  • The first routine cannot find NaN if it is the first element. – Ziyuan Jan 21 '16 at 13:07