Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created April 27, 2011 11:35
Show Gist options
  • Save aheckmann/944101 to your computer and use it in GitHub Desktop.
Save aheckmann/944101 to your computer and use it in GitHub Desktop.
a = 1;
console.error('global', typeof a); // undefined
const a; // const a is hoisted and set to undefined here first
a = 2; // has no affect
console.error('global after', typeof a); // still undefined
//const a = 1; // "TypeError: const 'a' has already been declared"
function whoo () {
const a = 2; // new execution context so this works
console.error('whoo', a); // 2
;(function inner () {
a = 3; // silently ignored
console.error('inner', a); // 2
})()
}
whoo();
a = 1;
console.error('global', typeof a, a); // number, 1
var a; // var a is hoisted and set to undefined here first
a = 2;
console.error('global after', typeof a, a); // number, 2
var a = 1;
console.error('global after', typeof a, a); // number, 1
function whoo () {
var a = 2; // new execution context
console.error('whoo', a); // 2
;(function inner () {
a = 3;
console.error('inner', a); // 3
})()
}
whoo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment