The horror-scope - Global, Local and Block scope in JS

ale3oula

Alexandra

Posted on June 17, 2019

The horror-scope - Global, Local and Block scope in JS

In programming, the scope of a variable determines its lifetime. The scope describes if a variable, a function or an object is accessible or inaccessible by different parts of the program during runtime. In this article, we will see examples to cover briefly the global, local and block scopes in JavaScript.

Wat are these blocks?

Before diving in the scope categories that exist in JS, we have to define first what a block is. A block is a piece of code inside a set of curly braces {} and groups code statements together. An example of a block could be a function, like this:

A function block

The Global scope

A variable exists inside or outside a block. If a variable is declared outside all functions or curly braces ({}), it exists in the global scope. The global variables can be accessed by any line of code in the program, including inside blocks.

A Global scope example

The Local scope

In contrast to global variables, locally scoped ones are only visible within the function they are declared. Each function written in JavaScript creates a new local scope and every variable declared in this scope is a local variable. That means that variables with the same name can be used in different functions. However, any effort to reference a local variable outside its scope will result in a Reference Error:

carbon-1

The Block scope

So far, we’ve seen variables defined with the var keyword. Var can declare a variable either in the global or local scope. The variables that are declared within the block scope are comparable to local ones. They are available within the block that they are defined.

The main difference between the local scope and block scope is that the block statements (e.g. if conditions or for loops), don't create a new scope. So the var keyword will not have an effect, because the variables are still in the same scope.

carbon-3

ES6 introduced block scope by using the let and const keywords. These two keywords are scoped within the block which are defined.

carbon-4

Why Scoping?

So, why having scopes and limit the visibility of variables? Firstly, security reasons. The variables are only accessible when they are needed. Secondly, the scope solves the namespace collisions problem, which happens when variables with the same name but with different scopes exist. Finally, It will save memory in your code because the block variables will stop to exist after the block finishes running.

Sum up

-> Global variables last as long as the application is running.
-> Local variables last as long as a function is running.
-> Block statements don't create a new scope and the block scope does not apply to var keyword.
-> Const and Let can define scope to block statements.

References:

Understanding scope in JS

JavaScript: A Basic Guide to Scope

Scope in Javascript

Understanding Variables, Scope, and Hoisting in JavaScript

JavaScript Scope and Closures

💖 💪 🙅 🚩
ale3oula
Alexandra

Posted on June 17, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related