Variable Shadowing Javascript
- What is variable shadowing?
- When a variable declared within a certain scope (e.g., a function or a block) has the same name as a variable in an outer scope. The inner variable "Shadows" the outer variable, making the outer variable inaccessible within the inner scope.
function test() {
let a = "Hello";
if (true) {
let a = "Hi";
console.log(a); // Hi
// variable a is shodowed in this block
}
console.log(a); // Hello
}
test();