DEV Community

Cover image for How Javascript Works!!!
namitmalasi
namitmalasi

Posted on

3 2

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.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay