Shadowing in JavaScript refers to the concept of a variable in an inner scope having the same name as a variable in an outer scope. When shadowing occurs, the inner variable temporarily hides the outer variable, affecting the access and visibility of the variables within the code. This phenomenon is closely related to the concept of variable scope in JavaScript.
In JavaScript, variables can be declared with the `var`, `let`, or `const` keywords. The scope of a variable determines where it can be accessed and how long it persists in memory. JavaScript has two main types of scope: global scope and local scope.
Global scope refers to variables that are accessible throughout the entire JavaScript code, regardless of where they are declared. On the other hand, local scope refers to variables that are only accessible within a specific block of code, such as a function or a block statement.
When a variable is shadowed, it means that a new variable with the same name is declared within a more nested scope, effectively hiding the variable with the same name in the outer scope. This can lead to unexpected behavior and bugs if not handled carefully.
Let's consider an example to illustrate shadowing in JavaScript:
javascript
var x = 10;
function myFunction() {
var x = 20; // shadowing occurs here
console.log(x); // outputs 20
}
console.log(x); // outputs 10
myFunction();
In this example, we have a global variable `x` with a value of 10. Inside the `myFunction` function, a new variable `x` is declared with a value of 20. When we log the value of `x` inside the function, it outputs 20 because the inner variable is in scope. However, when we log the value of `x` outside the function, it outputs 10 because the global variable is still accessible.
Shadowing can also occur with block-scoped variables declared using the `let` or `const` keywords. Consider the following example:
javascript
let x = 10;
if (true) {
let x = 20; // shadowing occurs here
console.log(x); // outputs 20
}
console.log(x); // outputs 10
In this case, we have a block statement (the `if` statement) that introduces a new block scope. Inside the block, a new variable `x` is declared and assigned a value of 20, shadowing the outer variable `x`. When we log the value of `x` inside the block, it outputs 20. However, when we log the value of `x` outside the block, it outputs 10 because the outer variable is still accessible.
It's important to note that shadowing can make code harder to read and understand, especially when variables have the same name but hold different values in different scopes. To avoid confusion and potential bugs, it's generally recommended to use different variable names or refactor the code to avoid shadowing.
Shadowing in JavaScript occurs when a variable in an inner scope has the same name as a variable in an outer scope, temporarily hiding the outer variable. This affects the access and visibility of the variables within the code. It's important to be aware of shadowing and handle it carefully to avoid unexpected behavior and bugs.
Other recent questions and answers regarding Examination review:
- Why is it generally recommended to limit the use of global variables in JavaScript?
- What happens when a function encounters a return statement?
- How are global variables accessed within functions?
- What is the difference between global and local scope in JavaScript?

