What is scoping?
Let's take a look:
hello variable is set outside of the following function. We consequently consider it to have a global scope. It is the most VIP of variables. It can go wherever it wants, even backstage with Niki Minaj (or whoever else the kids are listening to these days).
Since the hello variable has a global scope, we can access it inside of our
greeting variable. Thus, the invocation of the function stored in the greeting variable will log to the console the string stored in the
Now let's take a look at the reverse:
Notice that the function stored in the
salutation variable is attempting to call on a variable defined inside another function. The
hello variable does not have the right wristband for that.
In Memory Scopes
In memory scopes are also known as execution contexts. These are the memory systems a program creates as it runs to store variables and their values.
Let's take a look at how a program creates a data store as it runs, using our above example. I'm going to draw and you're not allowed to make fun of me:
First, the interpreter creates a memory store for the global variables. The variable
hello, which points to a string, is stored in memory along with its value. The variable
greeting, which points to a function, is stored similarly. At this point, the code within the function that
greeting points to is not being run, because it is not yet invoked. Our program, as it executes, is simply storing the knowledge that
greeting has a value of a certain function. Let's take a look below. On the left, we see our source code. On the right, we imagine our program as it is run in real time.
Second, the program will interpret the invocation of the greeting function:
greeting(). In doing so, it will enter the scope of the function stored by the
greeting variable. This means we are entering the local scope that is opened up within the global scope. Inside the local scope, our program needs to access the value of the
hello variable. So, it looks it up in the global space, where it was stored earlier in memory:
Thus, our program outputs "Sup bro?", a classy and mature greeting, to the console.
Interestingly, if we were to invoke the function stored in the
greeting variable again, we would have created two different objects. Each object would be stored separately in memory. Even though the two objects would appear the same and output the same greeting, they are not the same object.