Skip to content

Instantly share code, notes, and snippets.

@piperchester
Created July 7, 2017 13:18
Show Gist options
  • Save piperchester/d49b730a790d5d3a86fc92d391e09962 to your computer and use it in GitHub Desktop.
Save piperchester/d49b730a790d5d3a86fc92d391e09962 to your computer and use it in GitHub Desktop.
Hoisting demo
// hoisting sample
'use strict';
console.log(bar); // undefined
var bar = 'bar';
console.log(bar); // 'bar'
// function decls hoisted as well (not func expressions though)
// foo();
function foo() {
console.log(bam); // undefined
var bar = 'bam';
}
// console.log(bar); // ReferenceError: bam is not defined
// goo();
// will TypeError as decl is hoisted, assignment is done on interpreter's 2nd run
// so for example
// var goo;
// goo();
// goo = function () { ....
var goo = function() {
console.log(sam); // undefined
var sam = 'sam';
};
// func decls are hoisted before var decls
console.log(typeof boo); // 'function'
var boo = 'boo';
function boo() {
var lam = 'lam'
console.log(lam);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment