What’s the correct way to access x?
let x = 5
const f = (n:number)=> {
let x = "Welcome";
return x * n // First x not second x
}
and what’s the correct technical name for this operation?
What’s the correct way to access x?
let x = 5
const f = (n:number)=> {
let x = "Welcome";
return x * n // First x not second x
}
and what’s the correct technical name for this operation?
2
Answers
One way is to use block scope and OR operator.
Another way is to define
x
as a default paramter tof
The correct way to access a shadowed variable is not to shadow it. Refactor the code by renaming either variable to something else – typically the local one (in the inner scope).