Skip to content

Instantly share code, notes, and snippets.

@Reinmar Reinmar/gist:1159049
Created Aug 20, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.