Victoria Agbamoro
4 min readJan 27, 2020

--

The Basic Foundation of JavaScript Part 1: (Global and Local Scope)

Photo by African Girls Can Code Initiative

#myhundreddaysofcode

You want to spend quality time, understanding how JavaScript works, what makes our code run the way they do at the background. It Might seem little and tiring but it’s important that you spend your time to think of the process of each functionality and Syntax’s of the JavaScript language.

Before I began my #100 days coding challenge. I had spent two month trying to understand the JavaScript language, I got frustrated at some point, why? Because I was in a hurry to start building projects and not fully understanding the basic foundations of how JavaScript works in the background. Trust me it can be frustrating when you are trying to build projects and you don’t understand what you are really building.

Which is why it’s important to spend quality time understanding how JavaScript works.
And one thing you will need is Patience.

Before going into the major focus of this article, the aim is for you to be able to understand the following, which is outlined in the table of Content, which will be in bits. It will be great, if you follow through this series

Table of Content

  1. Scope

i) Local Scope

ii) Global Scope

2) Primitive Data Types

3) Operators

4) Variables

5) Expressions and Statements

6) Loops

7) Objects

8) Functions

The Computer is structured to run statements, Statements are a group of words, numbers, and Operators that performs some actions to be carried out. it is expected to do something for us, that is how JavaScript works. When we declare a variable and assign it a value, it is expected that when we log it to the console, it should as well print exactly what we assigned it.

a is a variable which is assigned a value to perform an operation of multiplication. This is a simple example of how JavaScript works.

What is Scope

Scope is determining where Variables, functions and objects are accessible during run time in our code. This means the scope of a variable, where it can be accessed is controlled by the location of where the variable was declared.

Scope is a very important concept in programming. Whenever you assign a variable to a value, you want to always access that value at any point in time in your code. That’s what Scope does. It’s answers the question “accessibility of Values “

Scope is often described as where the JavaScript engine looks for things. There are two important types of scope.

I) Global Scope

2) Local Scope

Global Scope

Any value declared in a JavaScript program is regarded as a Global Scope. A global scope is not limited to any function. Given that, it is declared outside of a function.It can be accessed any where in your code. I call it the “Universal Scope”.

Local Scope

Local Scope is often regarded as Function Scope. A local scope, i often describe it as “its own scope”. Given that any variable declared within that function is only accessible from that function and any nested functions. It cannot be accessible outside of that function.

Local Scope often gain more priority than the Global Scope.

If you declare a local variable and a global variable with the same name, the local variable will take precedence when you use it inside a function. This type of behavior is called shadowing. Simply put, the local variable shadows the universal variable.

Hope you have been able to understand the both concepts.😊

Block Scope

Block Scope is often used with expression and decision statements. Like the If statements and Loops. Block Scope is basically anything that is wrapped in curly braces.

It’s important to note that let and const have a block level scope, while var has a function scope.

I will describe the block scope as a proctective scope.Its helps avoid ressigning of values, especially when the same var is declared, using the let in a block scope, will help for a more organised code.

That’s is all for now!😀

I hope this was helpful, as a guide to understanding the fundamentals of JS.

The next series will be talking about Primitive Data types and Operators.

--

--

Victoria Agbamoro

Sharing my Interaction Design and writing story, one post at a time.