Skip to content

Instantly share code, notes, and snippets.

@joe-oli
Forked from tsiege/hoisting.js
Created November 26, 2019 20:30
Show Gist options
  • Save joe-oli/5dd977b5ed5b73473a14df6a0d5ecd56 to your computer and use it in GitHub Desktop.
Save joe-oli/5dd977b5ed5b73473a14df6a0d5ecd56 to your computer and use it in GitHub Desktop.
Hoisting examples
// what's hoisted
// var carName (just the variable name is hoisted)
// driveCar (whole function is hoisted because driveCar is a function declaration)
// var parkCar (just the variable name is hoisted because parkCar is a function expression)
console.log(carName);
// -> undefined
driveCar(carName);
// -> driving undefined
parkCar(carName);
// -> TypeError: undefined is not a function
var carName = "volvo";
// function declaration
function driveCar(carName){
console.log('driving ' + carName);
}
// function expression
var parkCar = function(carName){
console.log('parking ' + carName);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment