JavaScript scope

web
mobile

Scope determines the accessibility (visibility) of variables.

Remember: In JavaScript, objects and functions are also variables. Scope determines the accessibility of variables, objects, and functions from different parts of the code.


JavaScript Function Scope

In JavaScript there are two types of scope:

  • Local scope
  • Global scope

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

Scope determines the accessibility (visibility) of these variables.

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


Local JavaScript Variables

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.

Example 1


                      // code here can NOT use carName
      
                      function myFunction() {
                        var carName = "Volvo";
                      
                        // code here CAN use carName
                      
                      }
                  

Example 2


                      function myFunction() {
      
                        var carName = "Volvo";    // Variable declared within the function
      
                        console.log(carName);     // Result "Volvo" because we have access to the local variable carName
                      }
                      myFunction()
                
                      console.log(carName);       // Result error (carName is not defined) because we don't have an access to the local variable carName
                

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.


Global JavaScript Variables

A variable declared outside a function, becomes GLOBAL.

A global variable has global scope: All scripts and functions on a web page can access it. 


                      var carName = "Volvo";    // It's a global variable, because we declared it outside a function
      
                      // Any code here can use carName
                      
                      function myFunction() {
                      
                        // code here can also use carName 
                      
                      }
                  

Automatically Global

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

This code example will declare a global variable carName, even if the value is assigned inside a function.

Example 1


                      function myFunction() {
                        carName = "Volvo";      // Assigning a value to a variable that has not been declared
                
                        console.log(carName);   // Result "Volvo"
                      }
                      myFunction();
                
                      console.log(carName);     // Result "Volvo" even if it's outside the function
                  

Example 2


                      function myFunction() {
                        var carName = "Volvo";      // Assigning a value to a variable that has been declared
                
                        console.log(carName);   // Result "Volvo"
                      }
                      myFunction();
                
                      console.log(carName);     // Result error - carName is not defined
                  

Warning - Do NOT create global variables unless you intend to.

Note: 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.

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), but remain available to new pages loaded into the same window.


Rate this lesson

Previous lesson Next lesson

Spread the word:
Do you need help? Use our support forum

About the author

User avatar
Michal Szymanski
Co-Founder at MDBootstrap & BrandFlow. Entrepreneur, web developer, UI/UX designer, marketing analyst. Dancer and nerd in one.