-
-
Save joe-oli/5dd977b5ed5b73473a14df6a0d5ecd56 to your computer and use it in GitHub Desktop.
Hoisting examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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