DEV Community

Cover image for How Javascript Works!!!
namitmalasi
namitmalasi

Posted on

How Javascript Works!!!

"Javascript is a synchronous single-threaded language". JS is single-threaded which means only one statement is executed at a time. Synchronous execution usually refers to code executing in sequence. In sync programming, the program is executed line by line, one line at a time.

Everything in JavaScript happens inside an "Execution Context”. Execution Context can be assumed as a container or a body where JavaScript code is executed.

Execution Context has two components:
a) Variable Environment (a.k.a Memory)

All the variables and functions are stored in Variable environment in the form of key-value pairs. For example,

var firstVariable = 100; //line 1
function test() { //line 2
var secondVariable = 200; //line 3
console.log(firstVariable + " " + secondVariable); //line 4
}
test();

In the above code snippet, the variables firstVariable and secondVariable will be stored with their values and the function test will be stored with all its code as it is.

b) Thread of Execution (a.k.a Code)

In the Thread of execution, the JavaScript code is executed line by line.

var firstVariable = 100; //line 1
function test() { //line 2
var secondVariable = 200; //line 3
console.log(firstVariable + " " + secondVariable); //line 4
}
test();
The above code will be executed line by line from line 1 to line 4.

This was just an overview of how javascript works behind the scenes. I hope you would have found this article beneficial.
Thank you for reading through this article.

Top comments (0)