denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.97k stars 2.55k forks source link

Further wtf-ness with NaN being type number #88

Closed jordanallain closed 5 years ago

jordanallain commented 6 years ago

I thought you could expand on the NaN being a number type a little.

Type of NaN is a 'number':

typeof NaN; // -> 'number'
+ isNaN(NaN); // -> true

this is almost seemingly contradicting itself because it tells you that NaN is type number, but that NaN is indeed not a number. i understand it makes sense but at first glance i think it makes you think "wtf?"

also, NaN === NaN or NaN == NaN is false both times but may not be as applicable to this section

VarshaChahal commented 6 years ago

IEEE 754 standard, a standard for floating point arithmetic, defines arithmetic formats comprising of finite numbers, infinities and NaN(a special value). In simple words, NaN is a result of operations that yield values that cannot be represented or defined as floating point numbers. It is still a number, but we don't have a proper representation for it. Take an example of square root of a negative number, it is imaginary, therefore we represent it using NaN.

jordanallain commented 5 years ago

yes like i said i understand it makes sense but thought it still was appropriate for this repo.

denysdovhan commented 5 years ago

Feel free to open a PR where we can discuss this further.