Scope of Variables in JavaScript
                                        
                                                                                    
                                                
                                                    Last Updated : 
                                                    26 Sep, 2025
                                                
                                                 
                                                 
                                             
                                                                             
                                                             
                            
                            
                                                                                    
                In JavaScript, scope is the context that determines where variables can be accessed, helping write cleaner and error-free code.
            JavaScript
    // Declaring a global variable
let x = 10;
function func() {
    
    // Declaring a local variable
    let y = 20;
    // Accessing Local and Global
    // variables
    console.log(x,",", y);
}
func();
There are mainly different types of scopes of a variable. Let us understand these one by one.
1. Global and Local Scope
The image below shows Global and Local Scope in JavaScript to help understand their accessibility.
 Global Scope
A 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.
            JavaScript
    // Global Variable accessed from within a function 
const x = 10;
function fun1() {
    console.log(x);
}
fun1();
Explanation: In the program, the variables outside the function and now we can access those variables from anywhere in the JavaScript program.
Function(Local) Scope
A local variable is a variable declared inside a function, making it accessible only within that function. It cannot be used outside the function.
- Functions and Objects are also variables in JavaScript.
 
            JavaScript
    function fun2(){
    
    // This variable is local to fun2() and 
    // cannot be accessed outuside this function
    let x = 10;
    console.log(x);
}
fun2();
Explanation: In the Program, the code defines a function fun2 with a local variable x, which is accessible only inside the function, and prints its value when the function is called.
- Before ES6 (Released in 2015), variables were declared only with 
var, which is function-scoped (accessible within the function) and global Scoped (Accessible everywhere) and prone to issues like hoisting and global pollution. - let and const were introduced with ES6. Variables declared with let and const are either block scoped or global-scooped.
 
2. Block and Lexical Scope
The image below shows Block and Lexical Scope in JavaScript to help understand their accessibility.
 Block Scope
In JavaScript, block scope refers to variables declared with let or const inside a { } block. These variables are accessible only within that block and not outside it.
Variables declared with var do not have block scope. A var variable declared inside a function is accessible throughout that entire function, regardless of any blocks (like if statements or for loops) within the function.  If var is declared used outside of any function, it creates a global variable.
            JavaScript
    {
    
    // Var can Accessible inside & outside the block scope 
    var x = 10;
    
    // let , const Accessible only inside the block scope
    const y = 20;
    let z = 30;
    
    console.log(x);
    console.log(y);
    console.log(z);
}
console.log(x);
Explanation: In the Program, we have successfully accessed the variable with the var keyword because var does not have a block scope.
Lexical Scope
The variable is declared inside the function and can only be accessed inside that block or nested block is called lexical scope.
            JavaScript
    function func1() {
    const x = 10;
    function func2() {
        const y = 20;
        console.log(`${x} ${y}`);
    }
    func2();
}
func1();
Output:
Hello Geeks
Explanation: This code shows lexical scope where innerFunc accesses outerVar from outerFunc and prints "Hello Geeks".
Modular Scope: 
Introduced in  ES6, ES Modules are the standard way to organize JavaScript code into  reusable and maintainable chunks. Each module has its own scope, and  anything declared within a module is private by default unless  explicitly exported.
                                
                                
                            
                                                    
                            
                                
                                    
                                    
                                        
                                            
                                            Suggested Quiz
                                            
                                            
                                            
                                            
                                                6 Questions
                                            
                                         
                                     
                        
                                    
                                    
                                        
                                            
                                                                                                    
                                                        
                                                            What is the primary purpose of scope in JavaScript?                                                        
                                                        
                                                            
                                                                                                                                    - 
                                                                    
                                                                        
To determine the data type of variables
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
To manage memory allocation
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
To control the accessibility of variables
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
To optimize code execution speed
                                                                     
                                                                                                                            
                                                         
                                                        
                                                     
                                                                                                    
                                                        
                                                            Which type of scope allows a variable to be accessed throughout an entire JavaScript program?                                                        
                                                        
                                                            
                                                                                                                                    - 
                                                                    
                                                                        
Local scope
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
Block scope
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
Global scope
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
Lexical scope
                                                                     
                                                                                                                            
                                                         
                                                        
                                                     
                                                                                                    
                                                        
                                                            What is the main difference between variables declared with 'var' and 'let' in JavaScript?                                                        
                                                        
                                                        
                                                     
                                                                                                    
                                                        
                                                            Which of the following statements about local variables is true?                                                        
                                                        
                                                            
                                                                                                                                    - 
                                                                    
                                                                        
They can be accessed from anywhere in the program
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
They can only be accessed within the function they are defined in
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
They are automatically global variables
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
They cannot be used in nested functions
                                                                     
                                                                                                                            
                                                         
                                                        
                                                     
                                                                                                    
                                                        
                                                            What defines a block scope in JavaScript?                                                        
                                                        
                                                            
                                                                                                                                    - 
                                                                    
                                                                        
Variables declared with 'var'
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
Variables declared with 'let' or 'const'
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
Variables declared inside functions
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
Variables declared globally
                                                                     
                                                                                                                            
                                                         
                                                        
                                                     
                                                                                                    
                                                        
                                                            What is the characteristic of lexical scope in JavaScript?                                                        
                                                        
                                                            
                                                                                                                                    - 
                                                                    
                                                                        
It allows variables to be accessed globally
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
It restricts variable access to the block they are defined in
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
It enables access to variables from nested functions
                                                                     
                                                                                                                                    - 
                                                                    
                                                                        
It is only applicable to global variables
                                                                     
                                                                                                                            
                                                         
                                                        
                                                     
                                                                                                
                                                    
                        
                                                        
                                                            
                                                            
                                                            Quiz Completed Successfully
                                                            
                                                         
                        
                                                        
                                                            
                                                            Your Score :   2/6
                                                            
                                                            
                                                                Accuracy :  0%
                                                            
                                                         
                                                     
                                                 
                                             
                                         
                                     
                        
                                    
                                    
                                        
                                            
                                                Login to View Explanation
                                            
                        
                                            
                                            
                                                1/6                                            
                                            
                                            
                                             
                                                
                                                    1/6                                                
                                                
                                                    
                                                       < Previous
                                                    
                                                    
                                                        Next >
                                                     
                                                
                                             
                                            
                                         
                                     
                                 
                             
                                                                                
                                                            
                                                    
                                                
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics