HashTag JavaScript: How JavaScript works? & Execution Context?…

Shani Kumar Gupta
3 min readMar 20, 2021

Hey Folks, Nowadays JavaScript is the most popular and loved language amongst the developer groups. Why ?… Don’t worry I am here to explain. The reason behind the popularity of JS is simplicity, rich web development features, open standards, and community which helps JS to grow as a client-side and server-side programming language with the helps of different JS frameworks and libraries. Example- ReactJS, NodeJS, Angular, and many more …..

According to the survey taken by Stack Overflow in 2019, About 68% of developers used JavaScript as a primary language in their different projects. Sounds good.. Yes!!

Stack Overflow Survey 2019: Most Popular Technologies

So I am here with the fun and interactive series of JavaScript in which I will explain the basic concept which most of the developer phase problem while using JavaScript. So take a deep breath and stay because after this article you will definitely fallen love with JavaScript. Sounds good then let’s start -

Topic’s to be discussed in this article-

  1. How does JavaScript work? (Execution Context)
  2. Is JavaScript Synchronous or Asynchronous?
  3. Is JavaScript Single-threaded or Multi-threaded?

How does JavaScript work (An Execution Context)?

Always keep in your mind that-

Everything in JavaScript happens or executed inside an Execution Context.

So you might be thinking that what the hell is an Execution Context? If yes! then let’s go and explore an Execution Context with me…

Execution Context:

You can assume that an Execution Context is a box or a container in which the whole JavaScript code is executed.

Let’s explore with the help of a diagram-

Execution Context

As you can see, an Execution context looks like a container in which the whole JavaScript code gets executed.
It has two main component-
1. Variable Environment(Memory Component): Whenever we run our code variables and function are stored in this variable environment component and the variable is initially assigned with an undefined(JS pre-defined Keyword) value before the code started execution.

2. Thread of Execution(Code Component): The whole code resides in this component and code is executed in a sequential order one line at a time.

This is what an Execution context did. So this is the entry point in which firstly variables and function are assigned and stored in a variable environment and after that code is started execution one line at a time. Great, we have done with Execution context. Let’s explore further.

Is JavaScript Synchronous or Asynchronous?

JavaScript is Synchronous.

Synchronous means the whole JavaScript code will be executed in a specific order. Let’s explore more- So whenever we run our code, it starts execution from the top and goes to the next line when the current line finished execution.
JavaScript is only asynchronous in the sense that it can make, for example, Ajax calls. The Ajax call will stop executing and other code will be able to execute until the call returns, at which point the callback will run synchronously.

Is JavaScript Single-threaded or Multi-threaded?

JavaScript is Synchronous Single-threaded language.

As I have explained previously that JavaScript is a Synchronous language. Let’s deep dive and understand how JavaScript is a Single-threaded language.

It means JavaScript can only execute one command or statement at a time. This means it has one call stack and one memory heap. As expected, it executes code in order and must finish executing a piece of code before moving on to the next.

Congratulations to all…, we have done with a basic understanding of how JavaScript works, and Execution context, synchronous and asynchronous, and single-threaded or multi-threaded.

It might be confusing at the first time but keep reading these topics from other resources which are available on the internet which help you in long run.

So Let’s meet with another upcoming article next week and for the time being keep learning, keep safe and stay at home.

--

--