The scope chain helps JavaScript find variables by looking in the right places step-by-step.
Scope chain in Javascript
function outer() { let outerVar = 'hello'; function inner() { console.log(outerVar); } inner(); }
Variables are looked up starting from the current function, then moving outward.
If a variable is not found in the current scope, JavaScript checks the next outer scope.
sayHello uses the variable name from the outer function.function greet() { let name = 'Alice'; function sayHello() { console.log('Hello ' + name); } sayHello(); } greet();
check accesses a variable from the global scope.let globalVar = 'I am global'; function check() { console.log(globalVar); } check();
x, so it prints 2, then the outer prints 1.function outer() { let x = 1; function inner() { let x = 2; console.log(x); } inner(); console.log(x); } outer();
This program shows how the scope chain works with two variables named message in different scopes.
function outer() { let message = 'Hi from outer'; function inner() { let message = 'Hi from inner'; console.log(message); // inner message } inner(); console.log(message); // outer message } outer();
Each function creates a new scope for its variables.
If a variable is not found in the current scope, JavaScript looks outward until it finds it or reaches the global scope.
Variables declared with let and const are block-scoped, meaning they only exist inside the block they are declared in.
The scope chain is how JavaScript finds variables by checking current and outer scopes.
Inner functions can access variables from outer functions.
Variables with the same name in inner scopes hide outer ones.
Practice
scope chain in JavaScript help with?Solution
Step 1: Understand the role of scope chain
The scope chain is a mechanism JavaScript uses to look for variables starting from the current scope and moving outward.Step 2: Compare options with scope chain definition
Only Finding variables by checking current and outer scopes correctly describes this behavior; others describe unrelated concepts.Final Answer:
Finding variables by checking current and outer scopes -> Option BQuick Check:
Scope chain = variable lookup [OK]
- Confusing scope chain with asynchronous behavior
- Thinking scope chain sorts or styles elements
- Mixing scope chain with event handling
Solution
Step 1: Identify function-scoped variable declarations
Variables declared withvarare function-scoped, meaning they are accessible anywhere within the function.Step 2: Check accessibility inside a function
Declaringvar x = 10;inside a function limitsxto that function's scope.Final Answer:
var x = 10; -> Option CQuick Check:
Use var for function-scoped variables [OK]
- Using let or const which are block-scoped
- Confusing function declaration with variable declaration
let x = 5;
function outer() {
let x = 10;
function inner() {
console.log(x);
}
inner();
}
outer();Solution
Step 1: Understand variable shadowing in nested functions
The inner function looks forxin its own scope, then outer scopes. It findsx = 10inouter().Step 2: Determine the logged value
Sinceinner()uses thexfromouter(), it logs 10.Final Answer:
10 -> Option AQuick Check:
Inner function uses nearest x = 10 [OK]
- Thinking it logs global x = 5
- Expecting undefined because inner has no x
- Confusing with ReferenceError due to scope
function test() {
console.log(a);
let a = 3;
}
test();Solution
Step 1: Understand temporal dead zone with let
Variables declared withletare not accessible before their declaration inside their scope.Step 2: Analyze console.log usage before declaration
Usingabeforelet a = 3;causes a ReferenceError due to temporal dead zone.Final Answer:
ReferenceError because a is used before declaration -> Option DQuick Check:
let variables can't be accessed before declaration [OK]
- Assuming var behavior with undefined
- Thinking no error occurs
- Confusing with SyntaxError
let x = 1;
function outer() {
let x = 2;
function inner() {
let x = 3;
console.log(x);
}
inner();
console.log(x);
}
outer();
console.log(x);What is the output sequence?
Solution
Step 1: Trace inner function console.log(x)
Insideinner(),xis 3, so it logs 3.Step 2: Trace outer function console.log(x)
Afterinner(),outer()logs its ownxwhich is 2.Step 3: Trace global console.log(x)
Outside all functions, globalxis 1, so it logs 1.Final Answer:
3, 2, 1 -> Option AQuick Check:
Each scope logs its own x value [OK]
- Assuming all logs print global x
- Mixing order of outputs
- Ignoring variable shadowing in inner scopes
Ready to prove it?
Pass the test and this pattern moves unknown
