admin管理员组

文章数量:1335138

Can not quite understand the reasoning for this. In the following code the localStorage of an item is alerted as undefined, but if I use an if(x==undefined) syntax it does not work. Can somebody explain what is the problem. Thank you.

alert(localStorage["x"]);

if(localStorage["x"]=="undefined"){alert("y");}

The top line alerts undefined

The bottom line does not alert y for me.

Can not quite understand the reasoning for this. In the following code the localStorage of an item is alerted as undefined, but if I use an if(x==undefined) syntax it does not work. Can somebody explain what is the problem. Thank you.

alert(localStorage["x"]);

if(localStorage["x"]=="undefined"){alert("y");}

The top line alerts undefined

The bottom line does not alert y for me.

Share Improve this question edited Dec 20, 2012 at 1:28 Brett Gregson 5,9233 gold badges44 silver badges60 bronze badges asked Dec 20, 2012 at 1:23 Damien GoldingDamien Golding 1,0004 gold badges16 silver badges32 bronze badges 2
  • Duplicate?. Use if ( typeof window.localStorage['x'] != 'undefined' ) – elclanrs Commented Dec 20, 2012 at 1:26
  • 'undefined' !== undefined – Paul S. Commented Dec 20, 2012 at 2:26
Add a ment  | 

3 Answers 3

Reset to default 5

It doesn't contain the string "undefined", it contains a value of the type undefined:

if (localStorage["x"] == undefined) { alert("y"); }

The value undefined is possible to change in older browsers, so good practice is to check the type instead:

if (typeof localStorage["x"] == 'undefined') { alert("y"); }

Try:

if(typeof( localStorage["x"]) == 'undefined'){alert("y");}

OR

if( localStorage["x"]  == undefined){alert("y");}

OR

if( !localStorage["x"] ){alert("y");}

The two ways of checking for something being undefined are:

typeof foo === "undefined"

and

foo === undefined

In the first case, it will be true if foo was never defined or the value of foo is undefined.

In the second case, it will only be true if foo was defined (otherwise it'll break) and its value is undefined.

Checking its value against the string "undefined" is not the same at all!

UPDATE:

When I said that if you try to perform an operation on an object literal's property that isn't defined, I guess I meant if it's undefined at all, and what I meant was something more like this:

obj["x"].toLowerCase()
// or
obj["x"]["y"]

where you are attempting to access/operate on something that is originally undefined. In this case, simply paring in an if statement should be fine, because of the way object literals report the value...but is very different with normal Javascript variables.

With object literals, if a key (say "x") is not defined, then

obj["x"]

returns a value of undefined, so both the typeof and basic === undefined checks will work and be true.

The whole difference of not being defined or having a value of undefined is different with normal variables.

If you had:

var a;
// or
var a = undefined;

then both the typeof and basic === undefined checks I provided earlier would work and be true. But if you never even declared a, then only the typeof check would work and be true. The === undefined check would break.

Take a look at: http://jsfiddle/7npJx/

If you notice in the console, it says b is not defined and breaks the if statement.

Since you're basically looking at an object literal with localStorage, the way to distinguish whether an item is not defined or has a value of undefined is to use in first. So, you could use:

if (!("x" in localStorage)) {

to check if "x" is not a defined property at all, and:

else if (localStorage["x"] === undefined) {

to then check if it is defined but has a value of undefined. Then, using:

else {

would signify that localStorage["x"] is both defined and does not have the value undefined.

In your code though, it's okay to use the typeof or in checks (based on what you want to know) because of the way object literals report properties that aren't defined. Using the basic === undefined is also okay, but as Guffa pointed out, it's possible for the actual value of undefined to be overwritten and then wouldn't work in this parison. When it es to normal Javascript variables, typeof and === undefined checks aren't the same.

本文标签: javascriptlocalStorage alerts undefined but if undefined is falseStack Overflow