Open In App

Explain Scope and Scope Chain in JavaScript

In this article, we will try to understand what is the scope of a variable as well as its function (or method). We will see what is a Scope Chain with the help of certain coding examples.

Scope

Global Scope:

Example: In this example, we will be declaring a global variable which we will use in the later part of our code. We will call that variable in one function. We will call that function inside another function and then we will call that other function to see the result.






// Global Scoped letiable
let global_letiable = "GeeksforGeeks";
 
// First function...
function first_function() {
    return global_letiable;
}
 
// Second function...
function second_function() {
    return first_function();
}
 
console.log(second_function());

Output
GeeksforGeeks

Local or Function Scope:

Example: In this example, we will declare the main function which will consist of a local/function scoped variable. We will declare a nested function that will take that variable into consideration and perform a multiply operation on it. We will call the nested function inside the main function itself and thereafter the main function outside its declaration.



Then at last we will call our local/function scoped variable along with the local/function scoped function to see what output they will display upon their calling.




function main_function() {
 
    // letiable with Local Scope...   
    let a = 2;
 
    // Nested Function having Function Scope   
    let multiply = function () {
 
        // It can be accessed and altered as well
        console.log(a * 5);
    }
 
    // Will be called out when main_function gets called
    multiply();
}
 
// Display's the result...
console.log(main_function());
 
// Throws a reference error since it
// is a locally scoped letiable
console.log(a);
 
// Throws a reference error since it
// is a locally scoped function
multiplyBy2();

Output:

10
undefined
Uncaught ReferenceError: a is not defined

Block Scope:

Example: In this example, we will declare a block using curly braces “{ }”, and inside that block, we will declare a variable having a certain value in it. We will call that variable outside the blocked scope to see what output it actually displays upon calling.




{
    let x = 13;
}
// Throws a reference error
// since x is declared inside a block which
// cannot be used outside the block
console.log(x);

Output:

Uncaught ReferenceError: x is not defined

Scope Chain

Example: In this example, we will first declare a global scope variable which we will use in the later part of the code, and then we will declare the main function inside which we will do some stuff. We will declare another local/function scoped variable inside that main function and just after that we will declare two nested functions (having local/function scope) within the main function itself.




// Global letiable
let global_letiable = 20;
 
function main_function() {
    // Local letiable
    let local_letiable = 30;
 
    let nested_function = function () {
 
        // Display the value inside the local letiable
        console.log(local_letiable);
    }
 
    let another_nested_function = function () {
 
        // Displays the value inside the global letiable
        console.log(global_letiable);
    }
 
    nested_function();
    another_nested_function();
}
 
main_function();

Output
30
20

Article Tags :