Understanding scope and lexical scope is fundamental to writing efficient and error-free JavaScript code. These concepts dictate how variables are accessed and where they are available in your code.
Scope refers to the current context of execution, which determines the visibility and accessibility of variables. JavaScript has three types of scope:
Example:
{ let a = 10; const b = 20; console.log(a, b); // Output: 10, 20 } console.log(a); // Error: a is not defined
Example:
function testFunctionScope() { if (true) { var x = 10; // Function-scoped } console.log(x); // Output: 10 } testFunctionScope();
Example:
var globalVar = "I am global"; console.log(globalVar); // Output: I am global
Lexical scope means that the scope of a variable is determined by its position in the source code. Functions are executed using the scope chain that was in place when they were defined, not when they are invoked.
The scope chain is a hierarchy of scopes that JavaScript uses to resolve variable references. If a variable is not found in the current scope, it looks in the outer scope, continuing until it reaches the global scope.
Example:
function outer() { let outerVar = "I'm outer"; function inner() { console.log(outerVar); // Accesses the outer scope } inner(); } outer(); // Output: I'm outer
Inner functions have access to variables in their outer functions due to lexical scope.
Example:
function outerFunction() { let outerVariable = "Outer"; function innerFunction() { let innerVariable = "Inner"; console.log(outerVariable); // Outer console.log(innerVariable); // Inner } innerFunction(); } outerFunction();
function createMultiplier(multiplier) { return function (value) { return value * multiplier; // Accesses 'multiplier' from outer scope }; } const double = createMultiplier(2); console.log(double(5)); // Output: 10
Closures rely on lexical scope to remember variables from their outer environment.
Example:
function outerFunction() { let count = 0; return function () { count++; console.log(count); }; } const counter = outerFunction(); counter(); // Output: 1 counter(); // Output: 2
Variables declared without let, const, or var become global variables.
{ let a = 10; const b = 20; console.log(a, b); // Output: 10, 20 } console.log(a); // Error: a is not defined
Using var inside a block leads to unexpected results.
function testFunctionScope() { if (true) { var x = 10; // Function-scoped } console.log(x); // Output: 10 } testFunctionScope();
A variable declared in a nested scope can shadow (override) a variable in the outer scope.
var globalVar = "I am global"; console.log(globalVar); // Output: I am global
|
Lexical Scope |
||||||||
---|---|---|---|---|---|---|---|---|---|
Refers to the context in which variables are accessible. | Refers to how the location of variables in the code determines scope. | ||||||||
Can be global, block, or function. | Depends on the structure of the code when it is written. | ||||||||
Dynamic during runtime. | Fixed during code definition. |
function outer() { let outerVar = "I'm outer"; function inner() { console.log(outerVar); // Accesses the outer scope } inner(); } outer(); // Output: I'm outer
Mastering these concepts is critical for debugging and writing effective JavaScript code.
Hi, I'm Abhay Singh Kathayat!
The above is the detailed content of Mastering Scope and Lexical Scope in JavaScript. For more information, please follow other related articles on the PHP Chinese website!