Skip to content

Shaur-Repositories/Javascript_Scopes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This Repo maintains the demo for dev.to article : https://dev.to/shaur/scopes-in-javascript-with-devtools-16gp


An overview for Scopes in JS with the help of DevTools.

Before we start let's quickly go over some basic terminology.**

var : Use to declare function-scoped /local-scope or globally-scoped variable, optionally initialising it to a value.

let: The let statement declares a block-scoped local variable, optionally initialising it to a value. Redeclaration is not possible, but reassignment is possible.

const: Constants are block-scoped, The value of a constant can't be changed through reassignment, and it can't be redeclared. However, if a constant is an object or array its properties or items can be updated or removed.

block: when we want to treat the multiple lines of code as one aka compound statement we put it in '{}', a block is used with if..else, switch, loops But '{}' used with function is not block scoped but function-scoped. In strict mode, starting with ES6, functions inside blocks are scoped to that block. Prior to that, block-level functions were forbidden in strict mode.

closure: A closure is the combination of a function bundled together with references to its surrounding state (the lexical environment).

Scope: The current context of execution. The context in which values and expressions are "visible" or can be referenced.


Scope in Browser DevTools

We will be focusing on var, let and const and how they are scoped.

Types of Scope in JS

Types of Scopes

Fig : Showing Local, Closure, Script and Global present in Scope.


Global :

A var variable declared outside a function, becomes Global.

Script :

let and const variables declared outside a function or block, goes in Script.

When JS code Executes, in memory allocation phase var, let and const are given undefined values.

The catch is, we can access the var variable before initialising its value in the code execution part, till then it will give undefined.

But let and const will not behave the same and will give an error (ReferenceError) and are only accessible once they are defined, this is known as Temporal Dead Zone (TDZ).

Despite the pros and cons both Global and Script scope are at the same level i.e., they are present in the Global Execution Context (GEC).

Script and Global scope is always present via Scope Chaining.

GlobalScope_code

Fig : var, let and const declared at Global Scope.

GlobalScope_scope

Fig : Script and Global are created in scope.


Functional Scope :

Any variable defined inside a function becomes Local to its function and is not accessible outside. var, let and const all become Local scoped. Functions and blocks defined within the function have access to it.

functionScope_code

Fig : var, let and const declared in Function.

functionScope_scope

Fig : Local is created in scope for var, let and const.


Block :

let and const variables defined inside a conditional statement or loops come under Block scope. For var variables, if a block is defined outside the function it goes to Global scope and if it's inside a function it goes in the Local scope of the function. Also, if there is any name conflict with var outside the block, it will overwrite the outside var value. Block scope is destroyed once its execution is completed.

blockAtGlobal_code

Fig : var, let and const declared in Block at Global Scope.

blockAtGlobal_scope

Fig : Block is created in scope for let and const. While var a overrides the global value.


blockWithinFunction_code

Fig : var, let and const declared in Block in Function.

blockWithinFunction_scope

Fig : Block is created in scope for let and const. While var moves in the Local scope of Function.


Closure :

It's created for functions and contains the references of values which are not defined in the function's Local scope or in Global or Script, but are present in the functions in which this function is defined. It will only form Closure with those values which are used in this function.

closureAndHigherOrderFunction_code

Fig : Function being returned from a Function and forming a Closure in scope.

closureHigherOrderFunction_HigherORderFunctionScope

Fig : Scope of Higher Order Fucntion.

closureHigherOrderFunction_returnedFunctionScope

Fig : Scope of Returned Fucntion, forming Closure with the Local scoped values of Higher Order Fucntion.


closureNested_code

Fig : An Example of 3 level Nested Function forming Closure.

closureNested_GarpScope

Fig : Scope of grandparent Function.

closureNested_dragonScope

Fig : Scope of parent Function, Forming forming Closure with grandparent's Local scoped values which are used by it or by its child function.

closureNested_lufyScope

Fig : Scope of child Function, Forming forming Closure with parent's as well as of grandparent's Local scoped values which are used by it.


closureRefrence_code

Fig : Example showing Closure is a refrence to the value and not an Actual value.

closureRefrence_scope

Fig : Closure Formed with grandparent has the new value of var a.


BlockClosure_code

Fig : Higher Order Function with a Block.

BlockClosure_parentScopeBeforeBlock_Scope

Fig : Parent scope just before block completion.

BlcokClosure_child_Scope

Fig : Child function forming Closure with only var a from block as it was moved to Local scope of parent and let and const being Blocked scoped were destroyed.


Links :

Header Image : https://wallpapercave.com/wp/wp2742433.jpg

Dev.to Link : https://dev.to/shaur/scopes-in-javascript-with-devtools-16gp

Hosted Page : https://shaur-repositories.github.io/Javascript_Scopes/