Welcome to the Treehouse Community
Looking to learn something new?
Why does calling window.myVariable get me undefined when the variable hasn't been declared yet?
If I console.log(myVariable) before declaring it, I will get an Uncaught ReferenceError because myVariable is not yet defined. But when I console.log(window.myVariable), I get the value undefined, as if myVariable already has a memory space. What gives? Why don't I get a Uncaught ReferenceError?
I wasn't sure about this myself so I did some research on it.
I would have a look at that. In particular, the section "What is a variable?" and the sub-section "hoisting".
From "What is a variable?" 3rd paragraph:
"Already we can see the essential difference emerging. Properties belong to objects; Variables belong to contexts (and context happens to have an object representation – the VariableObject)."
From "hoisting" note 2):