JavaScript - Execution Context & Lexical Scope
Overview
Estimated time: 10–15 minutes
Execution context and lexical scope determine how variables and functions are resolved in JavaScript.
Learning Objectives
- Understand the call stack and execution context.
- Know how lexical scope affects variable resolution.
Prerequisites
Execution Context
function foo() {
var a = 1;
function bar() {
console.log(a);
}
bar();
}
foo();
Lexical Scope
function outer() {
var x = 10;
function inner() {
console.log(x);
}
return inner;
}
const fn = outer();
fn();
Common Pitfalls
- Confusing dynamic scope (not used in JS) with lexical scope.
Summary
Understanding execution context and lexical scope is key to mastering closures and advanced JS patterns.