What Is Scope?

In programming, scope is the area where variables, functions, and other name bindings can be used. Most simply:

var outerVariable = "Behold, outer scope!"

function middleFunction() {
    var middlingVariable = "Behold, middling scope!"

    function innerFunction() {
      var innerVariable = "Behold, inner scope!"
    }
}

In the above instance:

  • outerVariable can be accessed from anywhere
  • middlingVariable can be accessed from within middleFunction() or `innerFunction()
  • innerVariable can be accessed only from within innerFunction

That is to say: outerVariable has global scope, middlingVariable is scoped to middleFunction, and innerVariable is scoped to InnerFunction.

In almost all languages, a variable can be accessed by any function/class inside the function/class its currently in, but not outside/above it

For more information, see CodeCademy’s Scope Module

Leave a Comment

Your email address will not be published. Required fields are marked *