Skip to content

Instantly share code, notes, and snippets.

@git-ashish
Forked from max-mapper/0.md
Created April 29, 2017 09:47
Show Gist options
  • Select an option

  • Save git-ashish/d8fde5cf387542a3b0aaa25d8c2e42c6 to your computer and use it in GitHub Desktop.

Select an option

Save git-ashish/d8fde5cf387542a3b0aaa25d8c2e42c6 to your computer and use it in GitHub Desktop.
JS hoisting by example
// JS hoisting by example
// Do you fully understand each one?
// A (works)
function sayHi() {
console.log('hi!')
}
sayHi()
// B (works)
sayHi()
function sayHi() {
console.log('hi!')
}
// C (works)
var sayHi = function() {
console.log('hi!')
}
sayHi()
// D (does not work)
sayHi()
var sayHi = function() {
console.log('hi!')
}
// E (does not work)
(function sayHi() {
console.log('hi!')
})
sayHi()
// F (does not work)
sayHi()
(function sayHi() {
console.log('hi!')
})
// G (works)
(function sayHi() {
console.log('hi!')
})()
// H (works)
(function() {
console.log('hi!')
})()
// I (works)
var sayHi = (function() {
console.log('hi!')
})()
// J (works)
var sayHi = (function() {
console.log('hi!')
})
sayHi()
// K (does not work)
sayHi()
var sayHi = (function() {
console.log('hi!')
})
// editors note: because I write JS without semicolons I would
// never actually begin lines with ( in actual code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment