Question 18
Back to Exercise
What does the following JavaScript code print (console output, as in Node.js)?
function f() {
x = 5;
{
var x;
}
return x;
}
console.log(f(), typeof x);
- A 5 number
- B undefined undefined
- C ReferenceError
- D 5 undefined Correct
Correct Answer: Option D
var x is hoisted to the top of f (blocks do not limit var), so x = 5 assigns the local variable. No global x is created.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!