4

I have the following javascript code:

var currentIds = localStorage.getItem('currentPairsIds');

if ((typeof currentIds === "undefined") ||
    (currentIds == null))
        $.myNameSpace.currentIDs = new Array(3);
    else
        $.myNameSpace.currentIDs = currentIds.Split(',');

I'm debugging with Firebug and although currentIds hasn't got any value it always executes else statement.

UPDATE:

I'm getting this value from HTML5 storage.

What am I doing wrong?

VansFannel
  • 45,055
  • 107
  • 359
  • 626

7 Answers7

10

This is how I have solved my problem:

var currentIds = localStorage.getItem('currentPairsIds');

if ((currentIds === undefined) ||
    (currentIds == null) || (currentIds == "undefined"))
        $.myNameSpace.currentIDs = new Array(3);
    else
        $.myNameSpace.currentIDs = currentIds.split(',');

localStorage.getItem('currentPairsIds'); returns the string "undefined".

There is another error in Split() function. The right version is without any capital letter.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 3
    This is wrong, `localStorage.getItem()` does not return the string "undefined", it returns `null`, you only need the `null` check, not the other stuff that you're blindly testing. – Ruan Mendes Feb 27 '12 at 17:04
  • 2
    @JuanMendes, i got an undefined and this syntax worked for my problem. In fact, it was the odd `== "undefined"` that caught my problem! – Dave Alperovich Jan 28 '14 at 21:50
  • 6
    @DaveA That is because you saved something that was undefined, and local storage converts it into the string "undefined". When saving something into localStorage, you should be careful not to set it to undefined, or null. I would always set it to the empty string instead – Ruan Mendes Jan 29 '14 at 00:49
  • 1
    @JuanMendes, i did indeed save invalid data. App is in early development and sloppy things happen. But I don't think the string "undefined" went into memory. But things like this will happen. You can kill yourself with validation, but issues will crawl past you... – Dave Alperovich Jan 29 '14 at 00:59
  • 1
    @DaveA `localStorage.setItem('test', undefined)` will cause `localStorage.getItem('test')` to return `'undefined'`, the string. That can easily happen if you set it to a falsy value . That's what you should avoid – Ruan Mendes Jan 29 '14 at 01:50
  • @JuanMendes, of course avoid it. But also test for it. *&!@ will happen – Dave Alperovich Jan 29 '14 at 05:13
2

I would use a direct comparison instead of the notoriously odd "typeof" operator:

if ((currentIds === undefined) || (currentIds === null)) {
  //...
maerics
  • 151,642
  • 46
  • 269
  • 291
2

It's not working because localStorage.getItem returns null if the item is not defined, it does not return undefined http://dev.w3.org/html5/webstorage/#dom-storage-getitem

Example: http://jsfiddle.net/mendesjuan/Rsu8N/1/

var notStored = localStorage.getItem('ffff');

alert(notStored); // null
alert(typeof notStored); // object, yes, null is an object.

Therefore you should just be testing

alert(notStored === null);
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
0

In my case LocalStorage.getItem() was converting it to "undefined" as string. I also needed to check if it s "undefined" as a string.

var myItem = LocalStorage.getItem('myItem');
if(myItem != "undefined" && myItem != undefined && myItem != null){
    ...
}
bthn
  • 176
  • 2
  • 13
0

[Edit Edit Edit Edit :P]


currentIds = "undefined"

implies

typeof currentIds == "String"

Also see, Detecting Undefined, === isn't necessary for string comparison.

Community
  • 1
  • 1
Andrew
  • 13,757
  • 13
  • 66
  • 84
  • Your code is correct, the value of currentIDs as you described it doesn't seem to match what you expect it to be, the problem is somewhere else. – Andrew Jun 09 '11 at 16:01
  • I've updated my question this line `var currentIds = localStorage.getItem('currentPairsIds');` It's where I'm getting `currentIds` value. – VansFannel Jun 09 '11 at 16:19
  • @VansFannel you are retrieving an array then trying to call split on it, which is undefined. – Andrew Jun 09 '11 at 16:23
  • Your previous answer is the correct answer. localstorage.getItem returns the string "undefined". I have to check if currentIds == "undefined". – VansFannel Jun 09 '11 at 16:28
  • @Andrew: But I've solved using currentIds == "undefined". Your answer said currentIds = "undefined". – VansFannel Jun 10 '11 at 05:45
  • @VansFannel What I'm saying in my answer is that if currentIDs has a value of "undefined", then typeof will return String, not "undefined". I didn't tell you how to fix that, just what you were doing wrong, which was your question. – Andrew Jun 10 '11 at 13:46
0

I think you have to make checking for undefined comparing with == instead of ===. Example:

typeof currentIds == "undefined"

This will make sure, the variable is really undefined or not.

Nik Sumeiko
  • 8,263
  • 8
  • 50
  • 53
0
if( typeof(varName) != "undefined" && varName !== null )

be sure, you use ( " " ) quotes when checking with typeof()

qɐʇǝɥɐW
  • 347
  • 3
  • 17