GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Scope of Variables in JavaScript - GeeksforGeeks Scope of Variables in JavaScriptLast Updated : 4 Aug, 2026 Scope in JavaScript defines where a variable can be accessed or used within a program. It controls the visibility and lifetime of variables across different parts of the code.
There are several types of scopes of a variable. Let us understand these one by one. 1. Global and Local ScopeThe image below shows Global and Local Scope in JavaScript to help understand their accessibility. ![]() Global ScopeA global variable refers to a variable that is declared outside any function or block, so it can be used anywhere in the program, both inside functions and in the main code.
The variable x is declared in the global scope and can be accessed inside functions. However, global scope behavior may vary depending on the execution environment (e.g., browser vs module). Local ScopeA local variable is a variable declared within a function, making it accessible only inside that function. It cannot be used outside the function. Variables declared inside a block using
Here, the code defines a function
2. Block and Lexical ScopeThe image below shows Block and Lexical Scope in JavaScript to help understand their accessibility. ![]() Block ScopeBlock scope in JavaScript means variables declared with let or const inside { } are accessible only within that block, and accessing them before declaration (TDZ) causes a ReferenceError.
We have successfully accessed the variable with the var keyword because var does not have a block scope. Lexical ScopeLexical scope means a function can access variables from the scope in which it was defined. The scope is determined by the code structure, not by where the function is called.
Module ScopeModule scope refers to variables and functions that are accessible only within a specific JavaScript module. It helps keep code organized and prevents variables from affecting the global scope.
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |