Scope in JS

Scope in JS

Scope

  • Scope in JavaScript actually determines the accessibility of variables and functions at various parts in one’s own code or program.

  • In other words, Scope will help us to determine a given part of a code or a program, what variables or functions one could access and what variables or functions one cannot access.

  • Within a scope itself, a variable or a function, or a method could be accessed. Outside the specified scope of a variable or function, the data cannot be accessed.

Types Of Scope

  • Global Scope : Variables or functions (or methods) which are declared under a global namespace (like area or location) are determined as Global Scope. It means that all the variables which have global scope can be easily accessed from anywhere within the code or a program.
<script>
    // Global Scoped Variable
    var global_variable = "GeeksforGeeks";

    // First function...
    function first_function() {
        return global_variable;
    }

    // Second function...
    function second_function() {
        return first_function();
    }

    console.log(second_function());
</script>

Output:GeeksforGeeks
  • Lexical Scope : Variables that are declared inside a function or a method have Local or Function Scope. It means those variables or functions which are declared inside the function or a method can be accessed within that function only.
<script>
    function main_function() {

        // Variable with Local Scope...    
        var a = 2;

        // Nested Function having Function Scope    
        var 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 variable
    console.log(a);

    // Throws a reference error since it 
    // is a locally scoped function
    multiplyBy2();
</script>

Output:
10
undefined
Uncaught ReferenceError: a is not defined
  • Block Scope : Block Scope is related to the variables or functions which are declared using let and const since var does not have block scope. Block Scope tells us that variables that are declared inside a block { }, can be accessed within that block only, not outside of that block.
<script>
    {
        let x = 13;
    }
    // Throws a reference error 
    // since x is declared inside a block which 
    // cannot be used outside the block
    console.log(x);
</script>

Output:Uncaught ReferenceError: x is not defined

Thanks for Reading.