Question 05 / 14
Why is x initially a function and later a number?
Topic: Declaration instantiation
Declaration instantiation creates one binding, then later assignments replace the value stored in it.
Solve it with the execution visualizerStep through every binding, function call, and console output below.Predict the output
Track the value of the single x binding at each console.log.
console.log(x)
var x = 10
console.log(x)
x = 20
function x() {}
console.log(x)Reveal the answer and explanation
Expected output
[Function: x]
10
20Why this happens
The function declaration initializes x with a function object during declaration instantiation. The var declaration does not create a second binding with the same name.
Evaluating var x = 10 assigns 10 to the existing x binding, so the second log prints 10.
The later assignment replaces 10 with 20. The function declaration is not evaluated again at its source position.
What this question tests
- function declaration initialization
- duplicate var declarations
- binding reassignment
Runtime assumptions
Developer consoles format function objects differently; the first line represents the function x itself.