I want to check if a property exists in window, so I did this:
This code works:
if (window.foo) {
alert("exists");
}
else {
alert("doesn't exist");
}
Output:
doesn't exist
I thought the next code should work too because as far as I know when you define variables and you are not inside a function they are created as a property of the "window" object so this should be equivalent:
if (foo) { //it fails here "foo is not defined"
alert("exists");
} else {
alert("doesn't exist");
}
To my surprise it didn't work. My question is why it doesn't work if I don't prepend window?
I want to check if a property exists in window, so I did this:
This code works:
if (window.foo) {
alert("exists");
}
else {
alert("doesn't exist");
}
Output:
doesn't exist
I thought the next code should work too because as far as I know when you define variables and you are not inside a function they are created as a property of the "window" object so this should be equivalent:
if (foo) { //it fails here "foo is not defined"
alert("exists");
} else {
alert("doesn't exist");
}
To my surprise it didn't work. My question is why it doesn't work if I don't prepend window?
Share Improve this question edited Jul 3, 2013 at 22:23 Alfredo Osorio asked Jul 3, 2013 at 22:17 Alfredo OsorioAlfredo Osorio 11.5k12 gold badges59 silver badges85 bronze badges 5- 1 Where did you see that variables not defined in a function are automatically added to the window object? – Mike Commented Jul 3, 2013 at 22:19
-
3
if (typeof foo !== 'undefined')
– elclanrs Commented Jul 3, 2013 at 22:19 - 1 @Mike var myName = "Alfredo Osorio"; alert(window.myName); //Alfredo Osorio – Alfredo Osorio Commented Jul 3, 2013 at 22:20
- So what's your question? – fehnomenal Commented Jul 3, 2013 at 22:22
- @BenjaminGruenbaum nice that should be the answer. – Alfredo Osorio Commented Jul 3, 2013 at 22:25
2 Answers
Reset to default 7The first version is property access, it can't find the property so it returns undefined. The second is trying to access undefined variable .
Use what elclanrs suggested:
if(typeof foo !== "undefined"){
alert("exists");
} else {
alert("doesn't exist");
}
Why?
This is because of the GetValue
specified in the language specification.
.3. If IsUnresolvableReference(V), throw a ReferenceError exception.
This is what happens in the if(foo)
case, since foo
was not defined before.
On the other hand, if its an object the following happens:
- Let base be the result of calling GetBase(V).
- Let O be ToObject(base).
- Let desc be the result of calling the [[GetProperty]] internal method of O with property name P.
- If desc is undefined, return undefined.
So window.foo
returns the primitive language value undefined
which is falsy.
Think of the Window
object as a "giant closure", which encapsulates all the others. Now think about this:
var ext = "test";
function myScope() {
var ext = "foo"; //new variable in myScope's scope
console.log(ext); //foo
}
function changeExt() {
ext = "bar";//ext is not defined in this scope, so JS will look in the chain of scopes and set the value to the defined var in the global scope
console.log(ext);
}
myScope();
console.log(ext); //test
changeExt();
console.log(ext); //ext has been changed by changeExt to bar
Why is this happening? Because by using window.property
you're trying to access an undefined property of the window
object, not referencing to an undeclared var.
If you try to do the same thing without using the var
keyword in the global closure you will get the same result, because it is the last of the scopes chain.
Now let's think about it like a giant object:
var myObj = {prop: 'defined'};
Assuming that myObj
is the "global object", if you try to access prop
JS will redirect you to myObj.prop
(or it will try to do this), if the property (named for example) foo
isn't declared you will get your ReferenceError
.