Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 10
Q613 - Data Types
What does shadowing mean?
-
A
Declaring a global variable with the same name
as a previously declared global variable.
-
B
Declaring a local variable with the same name
as the previously declared global variable.
-
C
Changing the value of a variable.
-
D
Deleting and rewriting a selected piece of program code.
Reveal correct answer
Correct answer: B
Explanation
Topic: shadowing
Try it yourself:
- let x = 23;
- {
- let x = 42;
- console.log(x); // 42
- }
- console.log(x); // 23
Explanation:
Shadowing
When a variable is declared in a certain scope
having the same name defined on its outer scope
and when we call the variable from the inner scope,
the value assigned to the variable in the inner scope
is the value that will be stored in the variable in the memory space.
https://www.geeksforgeeks.org/variable-shadowing-in-javascript
Q613 (Please refer to this number, if you want to write me about this question.)
A. Declaring a global variable with the same name as a previously declared global variable would not be considered shadowing. Shadowing typically involves creating a new variable within a more specific scope that has the same name as a variable in a broader scope.
B. Shadowing occurs when a local variable is declared within a specific scope with the same name as a variable in an outer scope, such as a global variable. This local variable "shadows" the outer variable, meaning that references to the variable name within that scope will refer to the local variable instead of the global one.
C. Changing the value of a variable is not specifically related to shadowing. Shadowing refers to declaring a new variable with the same name as an existing variable, not modifying the value of an existing variable.
D. Deleting and rewriting a selected piece of program code is not related to the concept of shadowing. Shadowing specifically involves variable declaration and scope resolution, not code deletion or rewriting.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
