Scope
Scope decides where a variable can be seen. Think of it as rooms in a house: you can see out into the hallway, but people in the hallway cannot see into your room.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 27, 2026
Related tutorials
Learning objectives#
- Distinguish global, function and block scope
- Explain the scope chain
- Recognise shadowing
- Avoid polluting the global scope
Explanation#
Global scope: variables declared outside any function or block are visible everywhere.
Function scope: variables declared inside a function exist only inside it.
Block scope: let and const declared inside { } (an if, a loop, or a bare block) exist only inside those braces.
When code looks up a name, JavaScript searches the current scope, then the outer one, and so on — the scope chain. An inner variable with the same name “shadows” the outer one.
Code example: Rooms in a house#
const site = 'Techie Babaji'; // global
function showScope() {
const lesson = 'Scope'; // function scope
if (true) {
const tip = 'Blocks have scope too'; // block scope
console.log(site, '|', lesson, '|', tip);
}
// console.log(tip); // ReferenceError: tip is not defined
}
showScope();
let x = 'outer';
{
let x = 'inner'; // shadows the outer x
console.log(x);
}
console.log(x);
console.log(typeof lesson);
Techie Babaji | Scope | Blocks have scope too inner outer undefined
Step-by-step explanation#
- Inside the block, all three variables are visible because the scope chain looks outward.
- tip disappears when the if block ends.
- The inner x hides the outer x only inside its braces.
- lesson only exists inside showScope, so outside it is undefined.
Real-world use case#
Keeping helper variables inside functions prevents two scripts on the same page from accidentally overwriting each other’s data.
Common mistakes#
Assigning to an undeclared name (count = 1) creates a global in non-strict code. Always declare with let or const.
A const declared inside an if is gone after the closing brace.
Best practices#
- Declare variables in the smallest scope that needs them
- Avoid global variables
- Use ES modules — each module has its own scope
- Avoid confusing shadowing by choosing distinct names
Remember this#
Inner rooms can see outward; outer rooms cannot see inward.
let/const live inside the nearest { }.
Same name inside? The inner one wins (shadowing).
Practice exercise#
Your task: Predict the output, then run it: let a = 1; function f() { let a = 2; return a; } console.log(f(), a);
Show solution
let a = 1;
function f() { let a = 2; return a; }
console.log(f(), a); // 2 1
Mini quiz#
Where can a const declared inside a for loop’s body be used?
Want more? Practise all JavaScript MCQs · JavaScript interview questions
My notes#
Saved in this browser.