DEV Community

Harshita Gupta
Harshita Gupta

Posted on

What is JavaScript ? How is JS Code executed line by line?

According to MDN Docs, a high level definition of JavaScript is:

JavaScript is a scripting or programming language that allows you to implement complex features on web pages.

Everything in JavaScript happens inside Execution Context. All the JavaScript code is executed inside it.

The execution context is a big box that contains two components:

  1. Memory Component/Variable Environment
  2. Code Component/Thread of Execution

Memory Component: Here all the variables and functions are stored as key-value pairs.

Code Component: Here whole JS code is executed one line at a time.

The above explanation makes us reach to this definition:
JavaScript is a Synchronous Single-Threaded language

Let's break down the above definition by understanding these keywords:
Synchronous: In a specific order
Single-threaded: JavaScript can execute one command at a time.
It can only go to the next line once the current line has been finished executing.

Now let us understand how the execution context is created with the help of JavaScript program.

var n = 2
function square(num){
    var ans = num * num
    return ans
}
var square2 = square(n)
var square4 = square(4)
Enter fullscreen mode Exit fullscreen mode

When we run this whole code, a global execution context(GEC) is created. GEC is created in two phases:

  1. Memory Creation Phase
  2. Code Execution Phase

Memory Creation Phase
In this phase JavaScript will allocate memory to all the variables and functions.
Memory allocation starts from line 1 and goes till line 7 of the above code.

  1. As JS encounters line 1 it allocates memory to n. It stores a special value as undefined.
  2. It then see a function named square and allocation memory to variable square. Here it stores the whole code of the function inside this memory space.
  3. Similary it will allocate memory to square2 and square4 as undefined.
Memory Component Code Component
n: undefined
square: (num){
var ans = num * num
return ans
}
square2: undefined
square4: undefined

Code Execution Phase
The code again runs from line 1 to 7 after the memory allocation phase is finished.

  1. JS encounters line 1 and replaces n's value from undefined to 2
  2. Now from line 2 to 5 there is nothing to execute so it moves to line 6
  3. At line number 6 function is invoked, since function is a new mini program in itself so a new Execution Context is created.
    New execution context again has memory component and code component.

    1. Memory Allocation Phase

      1. It allocates memory to num: undefined
      2. It allocates memory to ans: undefined
      3. No more functions or variables so memory allocation phase is completed.
      Memory Component Code Component
      num: undefined
      ans: undefined
    2. Code Execution Phase

      1. When function is invoked with argument n then in line 1 n=2 which is passed as parameter to the function
      2. In the next line num * num is calculated inside code component and put inside ans in memory component.
      3. Now it sees return ans. When JS encounters return it returns the control back to the execution context where the function was invoked.
      4. Now the control goes line 6 again.
      5. The whole execution context for this instance of square function will now be deleted. alt text
  4. Now the square2 variable's undefined will be replaced with 4 (retuned as ans)

  5. Similary function will be invoked on line 7 and pass 4 as argument which will return 16 (with all the above steps repeating in the same order).

Image description

Now JS is done with executing the whole code, now the whole Global execution context is deleted.
This is how JavaScript code is executed line by line.

Thanks!

Top comments (2)

Collapse
 
77pintu profile image
77pintu

Thanks for the great post !

Collapse
 
harshitagupta profile image
Harshita Gupta

Thanks for appreciation