5

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?

8
  • Well that must mean that the value of currentIds is not really undefined. Commented Jun 9, 2011 at 15:56
  • @Pointy: it's undefined. The statement $.myNameSpace.currentIDs = currentIds.Split(','); throws an exception. Commented Jun 9, 2011 at 15:59
  • 1
    Is currentIds "undefined" or is it undefined? typeof "undefined" is string. Commented Jun 9, 2011 at 15:59
  • currentIds hasn't got any value. Commented Jun 9, 2011 at 16:00
  • 1
    Either you are stuck in an alternate universe, or the value of currentIds isn't what you think it is. Commented Jun 9, 2011 at 16:05

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
@JuanMendes, i got an undefined and this syntax worked for my problem. In fact, it was the odd == "undefined" that caught my problem!
@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
@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...
@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
|
2

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

if ((currentIds === undefined) || (currentIds === null)) {
  //...

Comments

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);

Comments

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.

2 Comments

== or === makes no difference in this case.
That changes nothing, typeof returns a String.
0

[Edit Edit Edit Edit :P]


currentIds = "undefined"

implies

typeof currentIds == "String"

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

6 Comments

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.
I've updated my question this line var currentIds = localStorage.getItem('currentPairsIds'); It's where I'm getting currentIds value.
@VansFannel you are retrieving an array then trying to call split on it, which is undefined.
Your previous answer is the correct answer. localstorage.getItem returns the string "undefined". I have to check if currentIds == "undefined".
@Andrew: But I've solved using currentIds == "undefined". Your answer said currentIds = "undefined".
|
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){
    ...
}

Comments

0
if( typeof(varName) != "undefined" && varName !== null )

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.