Created
November 11, 2014 16:06
-
-
Save tsiege/7ee3589898ea81d0453f 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