Skip to content

Instantly share code, notes, and snippets.

@0xAnon101
Last active September 24, 2018 11:08
Show Gist options
  • Save 0xAnon101/305902c4a982f4bbcab696db0515223a to your computer and use it in GitHub Desktop.
Save 0xAnon101/305902c4a982f4bbcab696db0515223a to your computer and use it in GitHub Desktop.
Breif Description of what happens in browser when a Javascript code executes?

How Does Javascript Run Your Code?

Google Chrome has V8 Javascript engine and Mozilla has SpiderMonkey Engine to run JS code.

var num = 2;
function pow(num) {
    return num * num;
}

Every time you run Javascript in a browser (or in Node) the engine goes through a series of steps.

One step is to create Global Execution Context.

Javascript engine and an Execution Context: How they work together ?

The engine creates a Global Execution Context every time you run some Javascript code.

Execution Context is a fancy word for describing the environment in which your Javascript code runs.

Global Execution Context is like a box inside Javascript engine.

Engine Works ?

Engine: Line one. There’s a variable! Cool. Let’s store it in the Global Memory.

Engine: Line three. I see a function declaration. Cool. Let’s store that in the Global Memory too!

Engine: Looks like I’m done.

##The Global Memory Contains Global variables and function declaration for later use.

When the Javascript engine runs your code it creates: a Global Execution context a Global Memory (also called Global Scope or Global Variable Environment)

The Global Memory Now contains :

 var num =2
 function pow(num) // declaration

Now when the function pow(num) is called , the Javascript Engine asks for help , and Call stack provides that help.

The Call Stack

    var num = 2;
    function pow(num) {
        return num * num;
    }
    var res = pow(num);

Current Call Stack Content after calling the pow(num) function , the push() method pushes the pow() function internally to stack after the main() or global scope function enters the stack , This main() is the where the code starts executing ( NOTE: MAIN() IS JUST AN ALIAS)

pow()
MAIN()

When pow() is called the Global Execution Context executes the pow() method inside it (REMINDER: This is where all javascipt code Runs) After that pop() operation occurs in the call stack

MAIN()

The Local Execution Context

  • Yet another thing has to happen when you run a function in Javascript. First, the function appears in the Global Execution context.
  • Then, another mini-context appears alongside the function:
  • That little new box is called Local Execution Context. This contains all the variables and function calls locally present inside the function pow(num) For e.g: num in parameter goes inside Local Execution Context.

When the above function is executed , a var res is being created with a value of UNDEFINED inside global memory.

During the execution phase a Local Execution Context gets created for holding up local variables.
   function pow(num) {
        return num * num;
    }

Therefore, a local execution context + local memory gets created which holds the num parameter Then as soon as pow() appears in the Global Execution Context for execution , the function executes and res takes its return value. And the output is shown into the screen.


Global and the Local Execution Context is the key for mastering Scope and Closures.

Wrapping up

The Javascript engine creates an Execution Context, a Global Memory, and a Call Stack. But once you call a function the engine creates a Local Execution Context which has a Local Memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment