Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Scope

JS Scope

Scope determines the accessibility of variables, objects, and functions from different parts of the code.

JavaScript has 3 types of scope:

  • Block scope
  • Function scope
  • Global scope
Block Scope

Variables declared inside a { } block cannot be accessed from outside the block.

					 
        
          {
            let name = "Lynxsia";
          }
          // name can NOT be used here
        
      

Variables declared with the var keyword can NOT have block scope and can be accessed from outside the block.

					 
        
          {
            var name = "Lynxsia";
          }
          // name CAN be used here
        
      
Local Scope

Variables declared within a JavaScript function, become LOCAL to the function.

Local variables have Function Scope. They can only be accessed from within the function.

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

Local variables are created when a function starts, and deleted when the function is completed.

					 
        
          // code here can NOT use name

          function myFunction() {
            let name = "Lynxsia";
            // code here CAN use name
          }

          // code here can NOT use name
        
      
Function Scope

JavaScript has function scope. Each function creates a new scope.

Variables defined inside a function are not accessible (visible) from outside the function.

Variables declared with var, let and const are quite similar when declared inside a function.

					 
        
          function myFunction1() {
            var name = "Lynxsia";   // Function Scope
          }
          function myFunction2() {
            let name = "Lynxsia";   // Function Scope
          }
          function myFunction3() {
            const name = "Lynxsia"; // Function Scope
          }
        
      
Global Scope

Variables declared Globally (outside any function) have Global Scope.

Global variables can be accessed from anywhere in a JavaScript program.

Variables declared with var, let and const are quite similar when declared outside a block.

A variable declared outside a function, becomes GLOBAL. All scripts and functions on a web page can access it.

					 
        
          var name = "Lynxsia";   // Global Scope
          let name = "Lynxsia";   // Global Scope
          const name = "Lynxsia"; // Global Scope
        
      
Automatically Global

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.

					 
        
          myFunction();

          // code here can use name

          function myFunction() {
            name = "Volvo";
          }
        
      
Strict Mode

All modern browsers support running JavaScript in "Strict Mode". In "Strict Mode", undeclared variables are not automatically global.

Global Scope And Window Object

In HTML, the global scope is the window object. We can access global variable (only var not let and const) via window object.

					 
        
          var name = "Volvo";    // code here can use window.name
          let name = "Volvo";    // code here can not use window.name
          const name = "Volvo";  // code here can not use window.name
        
      

Do NOT create global variables unless you intend to. Your global variables (or functions) can overwrite window variables (or functions). Any function, including the window object, can overwrite your global variables and functions.

The Lifetime of JavaScript Variables

The lifetime of a JavaScript variable starts when it is declared.

Function (local) variables are deleted when the function is completed.

In a web browser, global variables are deleted when you close the browser window (or tab).

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved