Advanced JavaScript: Chapter 2 - How javascript works?
Abhinandan Mishra
Specialist Programmer @Infosys | DSA, System Design | JavaScript, React, Angular, Next.js, .NET Core, FastAPI | Frontend, Backend | Web3
Understanding the internal workings of javascript becomes very important to become a good JavaScript developer.
In this article, we will understand the execution context. A good understanding of this concept will make it easier to understand crucial concepts like Hoisting, Scope chaining, closures, etc.
After reading this article you will have enough knowledge to understand the hard parts of javascript and grasp the core concepts easily. So, let's begin.
What happens on executing a javascript code?
When we execute a javascript code the syntax parser parses the code and then the compiler translates it to computer instructions(low-level language).
In JavaScript, the compiler is written in a way that it cares about where we have written the codes and based on that it assigns the memory.
In our previous blog, we explored the concept of scope chaining, which directly determines where a variable is defined and consequently, whether it can be accessed or not.
What is an execution context?
An execution context is basically an environment that gets created when a fragment of JavaScript code runs. It is created to handle all the transformations and execution of the code. It contains the currently running code and everything that helps in running that code.
There are basically two types of execution contexts:
Phases of execution context
There are two phases of an execution context:
function greet(){
console.log("Welcome to the blog!");
var surname_in_greet = "Mishra";
return surname_in_greet;
}
var name = "Abhinandan";
var surname = greet();
console.log(name+" ", surname)
return value is the value that will be returned by the function.
领英推荐
This is how the creation and execution of execution context works in the javascript.
Hoisting
Hoisting is referred to as the process in which the interpreter appears to move the declarations of functions and variables to the top of their scope prior to the execution of the code.
Now as we understand the creation phase of an execution context we can easily understand the hoisting.
Let's understand this by the previous example but with some small changes.
name = "Abhinandan";
var surname = greet();
function greet(){
console.log("Welcome to the blog!");
var surname_in_greet = "Mishra";
return surname_in_greet;
}
var name;
console.log(name, surname)
That is all these codes written below behave the same in javascript:-
Code1:
var name = "Abhinandan"
Code2:
var name;
name = "Abhinandan";
Code3:
name = "Abhinandan"
// because name is already declared in the creation phase
var name;
That's what hoisting is, that is the function and variables declarations are on the top of their scope ( obviously because of the creation phase of the execution context occurring before the execution phase of the execution context).
Whether let and const are hoisted or not?
So, one question that comes to mind of every person who's learning JavaScript is whether the let and const are hoisted or not.
Now, your question should be why this question arises.
The answer is that if we access the let and const declared variables before their declarations, the javascript engine throws an error.
Because let and const were introduced in ES6, the motive was to declare the block-scoped variables that can't be accessed out of the block. Block scoped variables are also hoisted but they are in the temporal dead zone till they get defined.
A variable in the temporal dead zone means that it has been declared in the creation phase of execution context but it is not accessible to the outer environment.
Once the block scoped variable gets declared it is moved out of the temporal dead zone and it becomes accessible.
Let's understand this temporal dead zone by the following example-
// block starts here
{
// name's TDZ starts here (at the beginning of this block’s local scope)
// name's TDZ continues here
// name's TDZ continues here
// name's TDZ continues here
// name's TDZ continues here
let name = "Abhinandan Mishra"; // name's TDZ ends here
// name’s TDZ does not exist here
// name’s TDZ does not exist here
}
// block ends here
So, the answer is that all the variables and functions are hoisted but the variables and functions declared with let and const are inaccessible before their definition.
That's it for this article, hope you have learned something new or brushed up your knowledge.
Thank you for reading the article.
?Aspiring Software Engineer ?Golang/Java && React ?Ex- Software Engineer Intern at EMSEC ?Ex- SDE [email protected] ?FullStack Developer ?ML/ AI Enthusiast ?Competitive Coder ?1700+ at LeetCode ?3 ?? at Codechef
1 年Loved it, awesome explanation Abhinandan sir ?? ?? ??
AASE @ Accenture
1 年Loved this article ????... Nice way to explain things...Smoother flow which makes the topics crystal clear ??... Thanks for sharing ??
Specialist Programmer @ Infosys | JavaScript, Angular, .NET Core | Full Stack Developer |
1 年Well explained ??
SDE - I @Expedia Group | Ex- SDE - Intern @Juspay | Siemens Scholar - Batch 07 | ICPC Regionalist '21
1 年Again an insightful article, thanks for sharing ?