[ solved ] Data types | Sololearn: Learn to code for FREE!

+24

[ solved ] Data types

NaN means "not a number" but why is typeof(NaN) a "number" ?

7/31/2019 9:16:35 PM

Mirielle[ Left SL ]

9 Answers

New Answer

+13

Unquoted literal constant NaN is a special value representing Not-a-Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number. Note − Use the is NaN() global function to see if a value is an NaN value. reference: https://www.tutorialspoint.com/javascript/number_nan https://code.sololearn.com/W2ueQjD1hg0l/?ref=app

+21

I could be wrong but I believe it belongs to the Number class which makes it a number type, but not an actual number

+16

Maybe it's like saying that a human is an animal that is not an animal. Humans belong to the category of animals but are not technically called animals. I don't know if that analogy is good.

+10

Math operations involve numeric value(s) as inputs yielding a numeric return value. Example: 10 / 2 returns 5 operation: division (/) inputs: 10 and 2 output: 5 In situations where the inputs can cause an invalid math operation, such as dividing by zero or evaluating the square root of a negative number, it will not be possible to yield a numeric value. Typically, invalid math operations would simply cause an exception. However, a placeholder value (NaN) could be used to indicate the result of an invalid math operation while preserving the type of the value as a number. ---- Simply restated, NaN is a placeholder value for number type variables that represent the result of an invalid math operation as an alternative to throwing an exception.

+6

NaN is the return value from operations which have an undefined numerical result. In JavaScript, aside from being part of the global object, it is also part of the Number object, Number.NaN. It is still a numerical type, but it is undefined as a real number.

+6

Oh joy another reference point to explain this better or at least cleaner: https://javascriptrefined.io/nan-and-typeof-36cd6e2a4e43 *hope this actually addresses your query

+5

According to its definition, NaN is number that reperesents undefined or unrepresentable number. From what I know, NaN is something like "special value", or an error flag, when the result of a calculation is undefined, like 0/0 or square root of negative one. So NaN is a number that is used to represent undefined number

+3

Younes BOUDOUL Interesting way to explain NaN. 😉 However, I'd say that NaN is ALWAYS a value of type Number that ALWAYS represents a value that is, eh hem... well... "Not a Number." 🤓

+1

you are right!! but sometimes NaN appear as a number ad that's why. for example when you type 3.235785684 * 11e512 he returns Infinity that's make infinity a number for JS But when you type (3.235785684 * 11e512)/(3.235785684 * 11e512) he should be returns 1 like 2/2 but he returns NaN if we realize it with the same logic as we did with infinity!! we should say NaN is an number BUT one of the most characters of numbers is order 0 => 1 => ...=> 999 ... & NaN is unordered: NaN < 1; // false // so he is a number sometimes and not a number in other cases