Hoisting Q2
What would this print out?
var food = "grapes"
var foodFun = function(){
console.log("original:", food);
var food = "sushi";
console.log("new:", food);
}
foodFun();//"original: undefined"
//"new: sushi"
// this would translate to
var food = "grapes"
var foodFun = function(){
var food;
console.log("original:", food); // undefined
food = "sushi";
console.log("new:", food); // sushi
}
foodFun();
// the function will keep its scope after hoisting and the `food` var
// will be replacedThoughts
Initially I thought about the function looking beyond its local scope into the global scope and getting
grapesbut that was wrong because I didn't think thatfoodwas going to be hoisted in the function scope.This proved to be wrong because the local variables ARE indeed hoisted as the function runs it EXECUTES hoisting when the function happens.
Last updated