Question 10 / 14

Does changing a parameter change the outer primitive?

Topic: Function calls

The parameter receives a copy of the number value, stored in a separate function binding.

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

Predict the output

Why do the inner and outer console.log calls print different values?

let a = 1
function b(a) {
  a = 2
  console.log(a)
}
b(a)
console.log(a)
Reveal the answer and explanation

Expected output

2
1

Why this happens

Evaluating b(a) reads the outer value 1 and uses that value to initialize the parameter binding named a.

Inside b, a = 2 changes the parameter binding, so the inner log prints 2.

The outer let binding is a different binding and remains 1 after the function returns.

What this question tests

  • argument evaluation
  • parameter initialization
  • separate lexical bindings

Runtime assumptions

The example uses a primitive. Passing an object would still copy a value, but that value would be an object reference.

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