0

typeof(Infinity) returns Number in Javascript.

But why doesn't x.isNaN, where x = 1/0, return Number ?

Sample code:

 var x = 1/0;

 document.write(typeof(x)); //returns Number

 document.write(x.isNaN); //return undefined

P.S: I am new to Javascript and learning it from W3Schools. I would be glad if you can direct me to any other reliable resources.

MincH
  • 3
  • 5
  • *"I would be glad if you can direct me to any other reliable resources."* There you go: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide, http://quirksmode.org/js/contents.html – Felix Kling Apr 28 '14 at 12:27

3 Answers3

0

Numbers (including NaN) don't have an isNaN method. It's global. What you want is actually isNaN(x). (Note the capital N.)

Scimonster
  • 32,893
  • 9
  • 77
  • 89
0

isNan is not a property of any number. isNaN, however, is both a function on the global scope, and on the Number object. (With the latter one being newer / more robust version)

Since isNaN is a function, you'll have to pass the variable you'd like to check as an argument:
isNaN(x) or Number.isNaN(x) will return true, as you expected.

The reason document.write(x.isNaN); returns undefined, is because x (a Number) doesn't have the property isNan.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 2
    Actually, `Number.isNaN` and `isNaN` are two different methods in JavaScript. – Benjamin Gruenbaum Apr 28 '14 at 12:22
  • I have edited my post. Sorry for the typo error in the post. would be glad if you can clarify in the current context of using isNaN(variable) where the variable = 1/0; Basically anything that would result in Infinity. – MincH Apr 28 '14 at 12:30
  • @MincH: The problem is: `x.isNaN`. `isNaN` is not a property. It's a function: `isNaN(x)` will work. – Cerbrus Apr 28 '14 at 12:32
  • @MincH: `isNaN(Infinity)` will return `false`, since `Infinity` is not `NaN`. What are you confused about? – Felix Kling Apr 28 '14 at 12:32
  • @Felix Kling typeof(Infinity) in Java script returns Number. Right? Then why doesn't isNaN(x), where x = 1/0, return true ? x will have infinity stored in it. – MincH Apr 28 '14 at 12:42
  • 1
    @MincHL: Yes, because `Infinity` is a value of the Number data type. *"Then why doesn't isNaN(x), where x = 1/0, return true ? x will have infinity stored in it."* Because `NaN` and `Infinity` **are two different values**. `isNaN` tests whether a value is `NaN` only, not `Infinity`. Maybe you are looking for `isFinite`, which tests whether a value is *not* `Infinity`. Why do you think `isNaN` should return `true` for `Infinity`? – Felix Kling Apr 28 '14 at 12:43
  • @MincH: Because `Infinity` is __not__ `NaN`. `isNaN` _only_ returns `true` if the variable is `NaN`, and _only_ then. Take a look at the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Examples) for results of `isNaN`. – Cerbrus Apr 28 '14 at 12:47
  • @Felix thank you very much for the explanation. I got it. var x = 4; // typeof(x) would return Number and isNaN(x) would return false, because x is a variable of type number as it is assigned to 4. similarly x = 1/0; will assign x to a value which is Infinity and Java script regards Infinity as a valid number. Hence typeof(x) would return Number and isNaN(x) would return false. I hope I have understood it just right. Thank you all again. – MincH Apr 28 '14 at 12:58
  • @MincH: I think you've got it, yea :-) As a quick reference: The **only** cases which should return `true` are: `isNaN(NaN)`, `isNaN(Number.NaN)`, `isNaN(0 / 0)`. Please consider accepting this answer if it has helped you. – Cerbrus Apr 28 '14 at 13:03
  • @Cerbrus 0/0 is not Infinity .. If I understand it correctly, it is undefined in mathematics and hence not a number and hence isNaN(0/0) returns true ! – MincH Apr 28 '14 at 13:30
  • @MincH: Exactly. However, I never stated that `0/0` equals `Infinity` – Cerbrus Apr 28 '14 at 13:31
  • @Cerbrus var x = 1000 / "Apple"; document.getElementById("demo").innerHTML = typeof(x) + "
    " + y + " "+ x; Running this gives me totally confusing outputs. typeof(x) gives Number value of x is NaN going back to typeof(x); how does it decide typeof(x) is a Number when the value it stores is NaN .. does this expression 1000 / 'abcd' evaluate to a number in Java Script ?
    – MincH Apr 28 '14 at 13:39
  • `var x = 1000 / "Apple"` results in `NaN`, so for `isNaN(1000 / "Apple")`, this case applies: `isNaN(NaN)`, so the result is `true`. _"how does it decide typeof(x) is a Number"_ It does **not**. If `isNaN(x)` returns false, that does not mean `x` is a `Number`. – Cerbrus Apr 28 '14 at 13:41
  • then how is type of x a number when 1000/"apple" evaluates to NaN. This means NaN is a number type value like 1 or 2 or 3 or 4...any number ? If yes then shouldn't isNan(NaN) return False ? – MincH Apr 28 '14 at 13:46
  • Let's make this as simple as possible: `isNaN` doesn't care if the parameter is a `Number` or not. It **only** checks if the parameter is `NaN`. It doesn't look at the parameter's *type*, only at it's *value*. – Cerbrus Apr 28 '14 at 13:47
  • Got it ! thanks for the explanation. So a final question, NaN is a number ? when I say var x = 100 / "a"; and typeof(x) is returning Number. I am inclined to understand that NaN is a number. – MincH Apr 28 '14 at 13:52
  • I see why there's a confusion. `typeof NaN` is indeed `number`. The only explanation I can give there is _"It was designed that way"_. I don't know why, but apparently, `Not a Number` is a number. If I had to guess, it was the best fit for a type. (`NaN` has nothing to do with strings, or arrays. number is the closest fit) – Cerbrus Apr 28 '14 at 13:53
  • So, @MincH, do you think this answers your question? – Cerbrus Apr 28 '14 at 14:01
  • Ok Cerbrus ! That indeed is confusing when you have to predict the outcome of a function, considering the reason for its existence and what it signifies. NaN means not a number. But when an expression evaluates to NaN and is stored in a variable, typeof that variable still returns Number. The whole idea crashes here. Anyways Its been an insightful discussion. I hope somebody has a better answer to this dichotomy. – MincH Apr 28 '14 at 14:03
  • @MincH: What is "a better answer" then? What is unclear? `NaN` does not mean it's type isn't `"number"`, it only means it's not a numeric value. – Cerbrus Apr 28 '14 at 14:11
  • Cerbrus, by better answer I was implying the very purpose of making a NaN a number (somebody involved closely in developing JS decided to make NaN a number). Your answer however has been quiet helpful in clearing my confusion and I guess that was it; like u said "If I had to guess, it was the best fit for a type. (NaN has nothing to do with strings, or arrays. number is the closest fit)". – MincH Apr 28 '14 at 14:16
  • @MincH: [This answer](http://stackoverflow.com/a/6678664/1835379) seems to answer _"why JavaScript considers NaN a number"_: _"The ECMAScript (JavaScript) standard specifies that Numbers are [IEEE 757](http://en.wikipedia.org/wiki/IEEE_754) floats, which include NaN as a possible value."_ (Basically: _"Because standard X says it is"_). Why that standard says so... I dunno xD – Cerbrus Apr 28 '14 at 14:23
0

isNaN is not a property, it is a global function or a Number static method (won't work on instantiated variables).

you'll have to use either:

isNaN(x);
Number.isNaN(x);

Preferencially, Number.isNaN, although it isn't supported in IE, Opera or Safari yet.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
  • Note that `Number.isNaN()` is part of ES6 and not supported in all browsers yet. – Felix Kling Apr 28 '14 at 12:36
  • Got you ! even in this case, Number.isNaN(x) does not return true. please correct me if i am wrong. – MincH Apr 28 '14 at 12:45
  • @Andre Understood. Infinity is considered a number in Java Script. So isNaN(Infinity) returns false because isNaN(variable) return true only when the variable is not a number. I am sorry for forcing upon my confusion. – MincH Apr 28 '14 at 12:53
  • @MincH isNaN(x) will return false because Infinity is still a Number type. – Andre Figueiredo Apr 28 '14 at 12:54
  • @AndréFigueiredo: No, it will return `false` because `Infinity` is *not `NaN`*. Also, `Number.isNaN(x);` seems to be working in chrome for me. – Cerbrus Apr 28 '14 at 12:54
  • @Cerbrus `Number.isNaN("a")` should return true, but in my v.34 it returns `false`. – Andre Figueiredo Apr 28 '14 at 12:56
  • @AndréFigueiredo: Eh, no, it shouldn't return `true`. The **only** cases which should return `true` are: `isNaN(NaN)`, `isNaN(Number.NaN)`, `isNaN(0 / 0)` – Cerbrus Apr 28 '14 at 12:58
  • @Cerbrus Infinity is not a `NaN`, i think just because it's a Number: Number(Infinity) will eval to Number Infinity. `typeof(Infinity) : "number"` – Andre Figueiredo Apr 28 '14 at 12:59
  • **`isNaN(Infinity)` returns `false` because `Infinity !== NaN`.** I don't know how I can make this any easier to understand. `NaN` is not a concept that defines whether or not a value is a number. It's a static value. A value that does not equal `Infinity`, so `isNaN`returns `false` – Cerbrus Apr 28 '14 at 13:01
  • I think that there's a confusion here.. I'm talking about isNaN, not Number.isNaN.. I'm talking about implementation of isNaN(), taht evaluates Number(parameter) to check the NaN. – Andre Figueiredo Apr 28 '14 at 13:22