This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

  • 7.9 JS Scope

    Scope determines the accessibility (visibility) of variables.

    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: JS Scope - Local Variables
    // code here can NOT use carName
    
    function myFunction() {
      var carName = "Volvo";
    
      // code here CAN use carName
    
    }

    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.
    Example 2: JS Scope - GLOBAL Variable
    var carName = "Volvo";
    
    // code here can use carName
    
    function myFunction() {
    
      // code here can also use carName
    
    }

    JavaScript Variables

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

    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 3: JS Global Variables
    myFunction();
    
    // code here can use carName
    
    function myFunction() {
      carName = "Volvo";
    }

    Strict Mode

    • All modern browsers support running JavaScript in "Strict Mode".
    • You will learn more about how to use strict mode in a later chapter of this tutorial.
    • In "Strict Mode", undeclared variables are not automatically global.

    Global Variables in HTML

    • With JavaScript, the global scope is the complete JavaScript environment.
    • In HTML, the global scope is the window object. All global variables belong to the window object.
    Example 4: JS Scope - Global HTML variables
    var carName = "Volvo";
    
    // code here can use window.carName

    Warning

    • 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.
    • 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).

    Function Arguments

    Function arguments (parameters) work as local variables inside functions.

    Navigate this module

    Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.



    JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon