search

Home  >  Q&A  >  body text

In JavaScript, except for NaN, are all other variables equal to itself?

How does javascript quickly determine whether the value of a variable val is NaN? If, except for NaN, all other variables are equal to itself, then just judge val===val directly?

学习ing学习ing2704 days ago1427

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-07-05 10:52:13

    NaN is the only non-reflexive value in JavaScript, which is

    NaN === NaN // false
    

    This book mentions:

    NaN is a very special value in that it's never equal to another NaN value (i.e., it's never equal to itself). It's the only value, in fact, that is not reflexive (without the Identity characteristic x === x) . So, NaN !== NaN.

    JavaScript you don’t know

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-05 10:52:13

    Yes, NaN can be judged using isNaN or whether it is equal to yourself

    At the same time, conversely, if two variables are equal to determine whether the two variables are the same, there are special cases of +0 and -0, and use the reciprocal to determine whether they are equal.

    reply
    0
  • 阿神

    阿神2017-07-05 10:52:13

    let a = []
    let b = []
    
    console.log(a===b)

    Would you like to give the question a try?

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-05 10:52:13

    ES6 provides a new Number.isNaN() method on the Number object. It is recommended to use Number.isNaN() directly to check whether a value is NaN.
    In addition, except for the cases of +0 and -0, === meets the needs. A better way is to use Object.is()

    reply
    0
  • Cancelreply