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?
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?
Share Improve this question edited Jun 9, 2011 at 16:18 VansFannel asked Jun 9, 2011 at 15:51 VansFannelVansFannel 45.9k117 gold badges375 silver badges652 bronze badges 8 | Show 3 more comments7 Answers
Reset to default 10This 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.
I would use a direct comparison instead of the notoriously odd "typeof" operator:
if ((currentIds === undefined) || (currentIds === null)) {
//...
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);
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.
[Edit Edit Edit Edit :P]
currentIds = "undefined"
implies
typeof currentIds == "String"
Also see, Detecting Undefined, === isn't necessary for string comparison.
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){
...
}
if( typeof(varName) != "undefined" && varName !== null )
be sure, you use ( " " ) quotes when checking with typeof()
currentIds
is not reallyundefined
. – Pointy Commented Jun 9, 2011 at 15:56$.myNameSpace.currentIDs = currentIds.Split(',');
throws an exception. – VansFannel Commented Jun 9, 2011 at 15:59currentIds
"undefined"
or is itundefined
?typeof "undefined"
is string. – Linus Kleen Commented Jun 9, 2011 at 15:59