Question 09 / 14

How can a function binding become a number?

Topic: Function calls

The function declaration initializes a, then the var initializer overwrites that same binding with 2.

Solve it with the execution visualizerStep through every binding, function call, and console output below.

Predict the output

At the typeof expression, is a still the declared function?

var a = 2
function a() {
  console.log(3)
}
console.log(typeof a)
Reveal the answer and explanation

Expected output

number

Why this happens

Declaration instantiation initializes a with the function object before statement evaluation begins.

The var declaration does not create another a binding, but its initializer is still an assignment that runs in source order.

After var a = 2 executes, a contains a number, so typeof a evaluates to "number".

What this question tests

  • shared binding name
  • function initialization
  • runtime assignment order

Runtime assumptions

This question concerns classic function and var declarations in one scope.

Specification references

JavaScript Code EditorDirectly edit or paste custom code
Step 0 of 0
StepSpaceRun
Press → (Step) or Space (Run)
Phase 1: Compilation(Hoisting & Memory Setup)
Phase 2: Execution(Line-by-line Evaluation)
Execution Step Breakdown

Press "→" / click "▶" to step line-by-line, or press "Space" / click "▶ Run" for auto-play.

Scope Chain
Lexical Environment Chain: Inner Scope → Outer [[Scope]] → Global (GO)
No active scope chain
CALL STACKStack of Execution Contexts (LIFO)
LIFO Stack Frames
Global Execution Context (GEC)
▶ Active Frame
HEAP & MEMORY SPACEAllocated Objects (GO, AO, FunctionObjects, ER)
ECMAScript Heap Area
No objects in memory
Console Output
console.log(...) Stream
No output generated yet