WebStorage for invalid key returns NaN instead of 0

  • The manual shows that when requesting a key that doesn't exist, the call will return a 0, however it's return NaN (not a number).

    Is there a way I can test if the value is NaN?

  • Good point, there's not currently any particular way to do that. I'll update the manual and add a condition for the next build.

    Edit: actually, it returns an empty string, not 0. If you try to convert an empty string to a number you'll get NaN. There are also existing conditions to check if a key exists, or you can check if the key returned an empty string before trying to convert to a number.

  • Good point, there's not currently any particular way to do that. I'll update the manual and add a condition for the next build.

    Edit: actually, it returns an empty string, not 0. If you try to convert an empty string to a number you'll get NaN. There are also existing conditions to check if a key exists, or you can check if the key returned an empty string before trying to convert to a number.

    Thanks... yeah, found a tutorial from September of this year and am now using the key exists calls to verify first... working like a charm.

    Thanks!

  • Still checking for NaN would be useful. I encountered a similar scenario where I was looking for a number from a browser string and it wasn't being reported. That's not a bug or anything, the browser string was empty... but the result was NaN and there was no convenient way to test for it.

    R

  • I think in that case, you should check for ""

    rho

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Aphrodite

    I did something similar, I checked for string length less than 2. ( I knew the number I wanted was always bigger than 100). I wasn't sure if checking for "" exactly matched what I was getting as there might have been a special character in there or something.. however they are probably the same thing.

    still getting NaN for the int of an empty string is acceptable... but I still think it's reasonable to check for the number NaN than check for the input condition.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)