- read

Internal working of JavaScript

Aryan Shinde 66

Functions are beautiful- Akshay Saini.

Making apps in JavaScript can be little frustrating if you don’t have idea about how JS works under the hood in the browser, JavaScript is the most loved and hated language in the world, so lets see how it works.

So, JavaScript while executing a code makes an global execution context which has two sides, one is the memory allocation and the other one is the, code execution phase and that is pushed on the call stack which is just a stack which holds all the function, remember whenever a function is called, an entire new execution context is created with memory allocation phase and code execution phase, we will look into it
Suppose we have this code

So here x is given a value of 10 and a function is called with a variable x with value 20 inside it, if we call the function even before executing it, that’s something called as hoisting (another beautiful concept) will work, and console.log x after the function, so JavaScript will run for the first time and assign the variables with undefined and assign the entire function declaration to that corresponding variable as per line number 2 and 6 respectively.
(You can consider like, a towel which Indians keep on the seat in bus to book it 😂 xD)
now comes the code execution phase, where every calculation and assignment of actual value is done, so here the value x is assigned 10 , before that we are printing x, so as we said it should have…. undefined!

and then directly executes the code as per the line number 4, now here is the beautiful part, now whenever a function is called that function is pushed on the call stack and an entire execution context is created with memory allocation phase and code execution phase,

Here now this function acts as an entire different program with all variables and function calls differently and now x is again given a value of undefined in the memory allocation phase and in the code execution phase, it is assigned its real value and when the last line of that function is executed by printing value of x, the function is popped off the call stack and that execution context is destroyed,

Our Final Output:

Similarly when the last line of the actual code is executed, entire global execution context is destroyed. It is soo beautiful!!

ever wondered how Async would be working in this world of single threaded language 👀 ,stay tuned, will cover that too!

Meme of the week!!

meme explanation: So JavaScript is weakly typed language which overloads the arithmetic operator and JavaScript searches for strings, if it finds it then JS converts another number on the side of operator to string and concatenates it, but only for + operator, for — operator it will do 1-”1"=0 xD

ik ik, it’s so beautiful isn’t it?

Thank you for reading it till here!

Stay Safe, Stay Happy!