Sitemap
Arsalan Khattak

Arsalan Khattak

Full Stack (MERN) Web Developer

JavaScript — Scopes

5 min readSep 20, 2019

--

Let’s take a look at Scopes in General and then in JavaScript.
We’ll learn how and where to access functions, variables, and objects in our code.

What Are Scopes?

Scopes are determining where variables, functions, and object lies in your code. Every variable, function, and object is scoped to a very specific place in your code. For our ease and to not make this blog very long, we will take a look at variables only, functions and objects work the same way. You might be able to access a variable anywhere in your code or you might be able to access it in a very short piece of code only. How to find a variable scope? Let’s learn it.

Types of Scope

There are two types of scopes in programming, the first one is Dynamic Scope and the other one is Lexical or static scope.

Press enter or click to view image in full size
Types of Scope

JavaScript uses Lexical scope, so let’s dive into it. We will also learn what is Dynamic Scoping and the difference between both as well later.

Lexical Scope

Scopes are further divided into two subtypes.
Global Scope
Local Scope

Global Scope

If a variable is defined at a global level we can say that it’s scope is global. Didn’t get it? Let me explain it.
If we define a variable at top of code, it can be accessed anywhere in the file.
For Example, we have an index.js

Global Variable — Arsalan Khattak

Here, we have a variable a which is defined at a global level thus its scope is global scope. We can access this variable anywhere in our file. If you execute this above code, you will get the following result

Result

Got it? Let’s take a look at another example and let’s also learn how the global variable will be access.

Global Scope — Arsalan Khattak

So here we have a nested function child inside mainFunc
When we execute mainFunc, it will call the child function, where it console log a.
Let’s see how this child function will access the global variable a.
First, it will find a in child, in this case, we don’t have any variable a, so it will jump to parent function mainFunc and find a in that function. Again, we don’t have a variable in function. It will jump to global scope (out of function) and there we have a, it will get the value and print it out.

Local Scope

Okay, let’s move on to local scope and see how does that works
A variable is defined to be a local variable if it is declared at a local level i.e inside a function and that variable is only scoped to that specific function.
Here is the first example

Local Scope — Arsalan Khattak

In this example, we have two a, one is inside function (local level) and the other one is outside (at a global level).
On executing this chunk of code, myFunc will be called, where a new variable a will be declared and then console.log(a) will be called which will look for an inside the function which we have in this case, so it will use that a and don’t move out of function scope. So the result will be “Inside Function”.

Get Arsalan Khattak’s stories in your inbox

Join Medium for free to get updates from this writer.

Here is a complex example (not really complex)

Local Scope — Arsalan Khattak

So in this chunk of code, we have a variable a declared three-time, one at global level, second and third at local functions. On calling myFunc a new variable myOtherFunc will be called and we will into that nested function. Here we will have third a in the stack. I think at this point you can explain which a will be called and how it will be called, right? but let me still try to explain.
So here console.log will search for a at the local scope which we have in this case and it will fetch the value and log it.
Imagine if we remove that declaration from nested function, in that case, a will be searched for inside the nested function, on not finding it, the search will be transferred to the upper level (myFunc) from where it’s value will be fetched.

Dynamic vs Lexical

Let’s quickly take a look at the difference, So

Dynamic scope means from where the variable is called from and Lexical Scope means where the variable is called at
Let’s understand this by an example

Dynamic Scope — Arsalan Khattak

In dynamic scope, this chunk of code will print 20 and in the lexical scope, it will print 10.
You already know how lexical scope will print 10, it will look for a in func, on not finding it, it will start looking for it in the global scope.
In case of Dynamic scope, it will look for a in func, on not finding it, it will look for it at the place from where func was called from which is func2, there we have it with the value of 20, so 20 will be printed.

--

--

Arsalan Khattak
Arsalan Khattak

Full Stack (MERN) Web Developer