Learn How JavaScript  Works.

Learn How JavaScript Works.

ยท

3 min read

Do You Ever Look at Some JavaScript Code and Wonder, "How does this code Work Inside browsers ?" ๐Ÿค”๐Ÿค” . Then It is the Time To Learn how Javascript Works.

Lets Start

JavaScript is one of the most used programming languages. JavaScript is the programming language of the Web and JavaScript is easy to learn. In the article Let's Learn about How JavaScript Works Inside Browsers.

Everything inside Javascript happens inside an Execution context.

There are 2 types of Execution context

  • Global Execution context
  • Function Execution context

Global Execution context

We learned that Everything inside Javascript happens inside an Execution context. When a new JavaScript File is loaded in the Web browser an execution context is Created Globally. We can call this Execution context as Global Execution Context

Function Execution Context

Inside Global execution Context When A Function is called, a new execution context will be created. This Execution context is known as Function execution context.

We Can say, In an execution context there are 2 parts

  • Variable Environment.
  • Thread of execution.

image.png

From the above Diagram, We can say that javascript codes are scanned and memory is allocated in a variable Environment In a Thread of Execution, code is executed one by one. Because Javascript is a synchronous single-treaded language.

Let's Understand execution contest with the help of some block of codes

var number = 2;
function square(num){
    let sqr= num * num
   return sqr;
}
square(number);

First, a global execution context will be created for the whole code in which all variables are scanned and made undefined and all functions will be stored as they are.

Here first the number and square function will be scanned and made available, then the number variable will get its value 2 after that when the function square will call, there will be a function execution context created on the top of the global execution context, and the whole process repeated for this execution context. In this example, we call the function square passing a parameter which is number =2 and this will be the argument num. Inside the function execution context, all variables are scanned and made undefined for the first time, and then it will get its value and return sqr after the execution of the code. After finishing the execution of the function the function execution context will be removed from the call stack. and other code will be executed if there is nothing the Global execution context also be cleared from the call stack.

#Conclusion

In this article, we have discussed how JavaScript code are being executed inside the Browser. I hope you find this article helpful. Please drop a comment so I could know someone has come this far. Thank you for reading.

ย