Why "typeOf null is object? TypeOf NaN is number and NaN==NaN is false?

8/22/2019 2:03:00 AM

Kensy Master Will

8 Answers

New Answer


First Haitian Coder In Suriname This will require additional research to further understand. But I'll post a general explanation to get you started. 1. null is actually a primitive value in Javascript. It isn't actually an object. My understanding is this is a bug that is an artifact from earlier versions of Javascript. It is unlikely to be changed anytime in the near or distant future as it would break a lot of code. https://2ality.com/2013/10/typeof-null.html 2. NaN is a special number value meant to indicate a value known to NOT be a number. It is the only way to identify a value for an invalid calculation without forcing an exception. Therefore, 0/0, Math.sqrt(-3), 0/"c", etc will all result in NaN instead of throwing an exception. The NaN value in Javascript is known as a quiet signal to indicate something went wrong. (Continued...)


Check out these links regarding IEEE 754 on the equality of NaN with itself: https://medium.com/engineering-housing/nan-is-not-equal-to-nan-771321379694 https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values/1573715#1573715 https://medium.com/engineering-housing/nan-is-not-equal-to-nan-771321379694 Sorry for the long explanations. I encourage you to do further research of your own for a deeper understanding.


3. The standard for floating point arithmetic (IEEE 754) stipulates that NaN will not preserve reflextivity of equality (NaN==NaN) or comparison operators (<, >, >=, <=). The reasons are a bit more involved than you may care to know. A simple explanation could be that while NaN states that it is Not a Number, it doesn't state what it is or could be. Since NaN can occur from an infinite number of invalid arithmetic scenarios that should essentially throw an exception, a comparison of equality cannot be asserted. Example: Although 0/0, 0/"c", and Math.sqrt(-3) all result in NaN, none of the following would make sense: Math.sqrt(-3) == 0/0 0/0 == 0/"c" However, notice that inequality can be asserted to be true: NaN != NaN Math.sqrt(-3) != 0/0 Truth be told, this is an over simplification that fails to explain the real mathematical axioms considered in the decision of IEEE 754. (continued...)


Fermi I am envious of your conciseness as I can be a bit long winded. 🤓 One minor correction in your answer for #1: "because the standard says so." According to the article below, typeof null returned "object" because of a bug that went undiscovered for a while in the typeof function written in C. Specifically, the type tags of Javascript values were stored in the lower 1 to 3 bits of 32 bit units. 000 mapped to the object type. The problem is, a JSVAL_NULL was the machine code NULL pointer, which happen to also use 000 in the lower bits. So, the typeof function mistakenly returned "object" for null. As you can see... this is a rather major bug that never got fixed because it was too late by the time it was discovered and much code on the internet has already been using this check. DOH! 🤦‍♂️😂🤣 https://2ality.com/2013/10/typeof-null.html


typeof null => object because the standard says so. https://stackoverflow.com/questions/18808226/ Everything which is compared to NaN will always return false. This is part of the standard defined by IEEE-754. https://stackoverflow.com/questions/1565164/ The IEEE-754 also states that 'Numbers' are floats, and NaN is a possible float value, so NaN is of type 'number' (although yes, NaN stands for Not-a-Number :>) . https://stackoverflow.com/questions/2801601/


https://www.sololearn.com/post/133451/?ref=app https://www.sololearn.com/post/133947/?ref=app https://code.sololearn.com/WKKkpq0efxai/?ref=app


David Carroll Ah, yes, I missed on that one. I often wonder how many of such major bugs went unfixed due to the sheer amount of code adapting it as a 'feature'. ... and not at all about conciseness! I always hope I could have researched more before posting, but it always ended with me trying to get fast answers.


Adi Pratama-Universitas Brawijaya