0

Can someone help me with the correct syntax for checking if a numeric value (or a variable that is meant to be numeric) is Nan, so I can set it to a numeric value?

I have

if (isNaN(sessionStorage.newDate)){
    sessionStorage.newDate=1984;
    }

But it returns the error 'isNan is not defined'

I'm using the html5 sessionStorage as a variable, but I don't think it's dependent on that as I can't get to to work with any variable.

(code above edited to be correct)

gavin stanley
  • 1,082
  • 2
  • 13
  • 28
  • 2
    `isNaN()`.......................... –  Jul 10 '12 at 19:38
  • [How do you check that a number is NaN in JavaScript?](http://stackoverflow.com/questions/2652319/how-do-you-check-that-a-number-is-nan-in-javascript?rq=1) Notice that the title is nearly identical to yours. Notice also that it would have been the first in the possible related questions provided when you typed the title, and it's also first in the *Related* section on the right. –  Jul 10 '12 at 19:39
  • possible duplicate of [Is there a (built-in) way in JavaScript to check if a string is a valid number?](http://stackoverflow.com/questions/175739/is-there-a-built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number) – jbabey Jul 10 '12 at 19:41
  • `sessionStorage.newDate.toString() === "NaN"` – Engineer Jul 10 '12 at 19:41
  • Hi I checked that post, for sure, but was looking for it within an if statement, which that post didn't provide. I realise now it was due to the casing (sorry!) and that the if statement construction was fine. Sorry about that – gavin stanley Jul 10 '12 at 19:44
  • @Engineer: What if `sessionStorage.newDate` is `null` or `undefined`? –  Jul 10 '12 at 19:44
  • I've changed the post title to be more specific, and differentiate from the other post you mentioned – gavin stanley Jul 10 '12 at 19:49
  • @amnotiam What if the OP's code is in `try/catch` block?? – Engineer Jul 10 '12 at 19:50
  • @Engineer: What's your point? The issue is that your technique will fail if it isn't in a try/catch. Just because it *might* be doesn't make it a good technique. Also, it skews the result. How about: `"abc".toString() === "NaN"; // false` or `"NaN".toString() === "NaN"; // true` –  Jul 10 '12 at 20:00
  • @amnotiam I think, my technique is suitable in this situation, because `newDate` is always `Number`, I guess. – Engineer Jul 10 '12 at 20:02
  • @amnotiam Also consider, that `isNaN('NaN')` evaluates to `true`, like my *technique* `"NaN".toString() === "NaN"` – Engineer Jul 10 '12 at 20:04
  • @Engineer: If you can somehow guarantee that it's a number, then I wouldn't see a problem. But if that guarantee can be made, then a `NaN` test probably isn't needed. WRT `isNaN('NaN')`, we would expect a `true` result, since that's a string that doesn't coerce to a numeric value. –  Jul 10 '12 at 20:07
  • ...I see what you mean. My second example didn't make sense. –  Jul 10 '12 at 20:08
  • @amnotiam The OP in his question have mentioned about numeric values.So I can assume that he/she works with numbers. WRT your both samples do not make sense. I have done some researches, and I might say, that `isNaN` is not reliable enough.Look [here](http://stackoverflow.com/questions/11430444/what-does-v-v-expression-check/11430466#11430466), could be interesting.Even `String(sessionStorage.newDate)==="NaN"` is more reliable than `isNaN`. – Engineer Jul 11 '12 at 15:15
  • @Engineer: The first example makes perfect sense. `"abc"` cannot be successfully coerced to a number, so it should return `true`. You're assuming OP doesn't want the value considered as its toNumber evaluation. Whether or not `isNaN` is the right solution for the task depends on what OP wants. I have no idea why you consider `isNaN("NaN")` and `isNaN(undefined)` to be failures. Neither are coercible to a Number, so `isNaN` should return `true`. Just because you think `isNaN` should behave differently, doesn't mean those are failures. –  Jul 11 '12 at 15:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13746/discussion-between-engineer-and-am-not-i-am) – Engineer Jul 11 '12 at 15:32

3 Answers3

3

isNaN(value) the last "n" in the function name is capitalized

Oswaldo Acauan
  • 2,730
  • 15
  • 23
1

The name of the function you're looking for is isNaN() (case matters in Javascript).

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

You have got a case problem.

You put:

isNan(sessionStorage.newDate)

and, you has to put:

isNaN(sessionStorage.newDate)

NaN, not nan.

Imaky
  • 1,227
  • 1
  • 16
  • 36