What is the difference between (NaN != NaN) and (NaN !== NaN)?

前端 未结 5 1064
旧时难觅i
旧时难觅i 2020-12-12 13:32

First of all I want to mention that I know how isNaN() and Number.isNaN() work. I am reading The Definite Guide by David Flanagan and he g

5条回答
  •  误落风尘
    2020-12-12 13:58

    I just want to point out NaN is not the only thing that produces x !== x without using the global object. There are lots of clever ways to trigger this behavior. Here is one using getters:

    var i = 0, obj = { get x() { return i++; }};
    with(obj) // force dynamic context, this is evil. 
    console.log(x === x); // false
    

    As other answers point out, == performs type coersion, but in as in other languages and par the standard - NaN indicates a computation failure, and for good reasons is not equal to itself.

    For some reason beyond me people ocnsider this a problem with JS but most languages that have doubles (namely, C, Java, C++, C#, Python and others) exhibit this exact behavior and people are just fine with it.

提交回复
热议问题