Skip to content

Instantly share code, notes, and snippets.

@Reinmar
Created August 20, 2011 12:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Reinmar/1159049 to your computer and use it in GitHub Desktop.
Save Reinmar/1159049 to your computer and use it in GitHub Desktop.
Hoisting, function declarations vs function expressions
console.log('--------- Hoisting:');
(function () {
//console.log('3.1:', a); // -> ReferenceError
console.log('3.2:', b); // -> undefined
var b = 1;
console.log('3.3:', c); // -> undefined
if (false) {
var c = 1;
}
console.log('3.4:', c); // -> undefined
var d = 10;
(function () {
if (d === 10) {
var d = 20;
}
console.log('3.5:', d); // -> undefined
})();
})();
console.log('--------- FD vs FE:');
(function () {
a('4.1:'); // -> 2
var a = function (i) {
console.log(i, '1');
};
function a(i) {
console.log(i, '2');
}
a('4.2:'); // -> 1
function b() {
console.log('4.3: 1');
}
function b() {
console.log('4.4: 2');
}
b(); // -> 2
var c1 = function c2(i) {
console.log(i, c1, c2);
};
c1('4.5:'); // -> function, function
//c2('4.6:'); // -> ReferenceError
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment