whatstheoutput_

#90: A parameter with the same name as an outer variable

Topic: scope · Difficulty: ★

let value = 'outer';
function show(value) {
  console.log(value);
}
show('inner');
console.log(value);

Output

inner
outer

Why

A function's parameters are local variables scoped to that function's body, declared fresh on every call. Naming a parameter value the same as an already-existing outer value does not connect the two in any way — it just shadows the outer one for the duration of the call, exactly like a let redeclaration in a nested block would. Inside show, value refers only to the argument passed in, 'inner', which is what gets logged. The outer value was never touched, so after the call returns, logging it at the top level still prints 'outer'.

More scope puzzles

Play today's puzzle →