Predict the output of the following JavaScript code
function test() {
var a = 1;
function func() {
console.log(a);
var a = 2;
}
func();
}
test();
Quiz Explanation
The variable a is hoisted within the func function, meaning the declaration var a is moved to the top of the function scope. However, the assignment a = 2 is not hoisted. Therefore, console.log(a) references the hoisted variable a before it is assigned any value, resulting in undefined.