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
1Why 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.