Skip to content

Instantly share code, notes, and snippets.

@micmath
Created August 2, 2012 07:32
Show Gist options
  • Save micmath/3234884 to your computer and use it in GitHub Desktop.
Save micmath/3234884 to your computer and use it in GitHub Desktop.
Examine the code snippet below, without evaluating it. What would you expect to be consoled?
MyApp = 42;
(function() {
if (typeof MyApp === 'undefined') {
var MyApp = { oops: true };
}
console.log(MyApp);
}());
@kai-koch
Copy link

kai-koch commented Aug 2, 2012

[Object]
Since the inner var declaration masks the global MyApp.
The declaration of var MyApp in the function happens before the If-statement, and there for is undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment