Predict Scope Shadow Output
Predict Output Medium
Question
What does this code print?
Code
let x = 10;
function test() {
let x = 20;
console.log(x);
}
test();
console.log(x);
Hint
The local x inside the function shadows the global x.
Explanation
Inside the function, let x = 20 creates a new local variable that shadows the global x = 10. So console.log(x) inside the function prints 20. Outside the function, the global x is still 10. The output is: 20 (from inside) and 10 (from outside).
Related Resources
Related Lessons
- Scope and Visibility
Learn about scope and variable visibility.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.