DEV Community

Cover image for How JavaScript works behind the scenes?
Amol Shelke
Amol Shelke

Posted on

How JavaScript works behind the scenes?

Everything in javaSscript happens inside an execution context

  • Execution context

Execution context is like an big box and it has two component in it.

Image description

  1. The first component is known as memory component so this is a place where a variable and function are store as an key value pair and it is also know as Variable environment

  2. The second component of the execution context is the code component so this is the place where the code is executed one line at a time. and it is also know as thread of execution

JavaScript is a synchronous single threaded Language

The single threaded means that JavaScript can only execute one command at a time. and in a specific order the next line of code will execute after the first line of code. As soon as a JavaScript program run a brand new execution context will created.

It has two component:-

  1. memory creation phase
  2. code execution phase
var a = 2;
 function square(num){
  var ans = num * num;
  return ans;
}

var square2 = square(num);
var square4 = square(4)
Enter fullscreen mode Exit fullscreen mode
  1. Memory Creation Phase:
    In Memory Creation Phase we are allocating all variable and function in global space inside the whole program and we allocate the variable with undefined and in case of function we just stored the function as it is.

  2. Code Execution Phase:
    In this phase the JavaScript being single threaded language will run line by line and allocate the value of a variable and update the values of variable and function.

Top comments (0)