最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checking undefined value not working? - Stack Overflow

programmeradmin0浏览0评论

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
  • Well that must mean that the value of currentIds is not really undefined. – Pointy Commented Jun 9, 2011 at 15:56
  • @Pointy: it's undefined. The statement $.myNameSpace.currentIDs = currentIds.Split(','); throws an exception. – VansFannel Commented Jun 9, 2011 at 15:59
  • 1 Is currentIds "undefined" or is it undefined? typeof "undefined" is string. – Linus Kleen Commented Jun 9, 2011 at 15:59
  • currentIds hasn't got any value. – VansFannel 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. – Andrew Commented Jun 9, 2011 at 16:05
 |  Show 3 more comments

7 Answers 7

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论