What is the difference between undefined and window.undefined in JavaScript?

If a is undefined, this works:

 if(window.a) {} 

while this causes an error:

 if(a) 

Can someone explain why?

+6
javascript undefined
source share
1 answer

window.a is a property of window , and undefined. a is a variable, and it is not declared.

To use a variable, you must first declare it with the var statement. Since you did not declare a , the interpreter throws an error. To use the properties of objects, you do not need to explicitly specify them. Crockford writes in Good Parts:

If you try to extract a value from an object, and if the object does not have a member with that name, it instead returns undefined.

+10
source share

All Articles