Amay Jain
Amay Jain (@BrajBliss)


Amay Jain (@BrajBliss)


Most Asked JavaScript Interview Questions - 1

We will see the most asked basic to advanced JavaScript interview questions in this first set.

Amay Jain's photo
Amay Jain
·Aug 14, 2022·

3 min read

Most Asked JavaScript Interview Questions - 1

Photo by Oskar Yildiz on Unsplash

Play this article

Table of contents

  • What is Hoisting?
  • Var vs Let vs Const
  • What is an Event Loop?
  • setTimeout vs Promise precedence
  • setTimeout vs setInterval
  • List few ES6 Features

What is Hoisting?

First, let us see what MDN Web Docs says:

JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables, or classes to the top of their scope, prior to execution of the code.

In simple terms, Hoisting is the mechanism that enables us to use variables, functions, and classes before declaration. When JavaScript code is executed, it creates the global execution context. It has two phases namely creation and execution. Hoisting takes place during the creation phase.

For example, take a look at this piece of code:

const hashnode = "best blogging platform"

Now, this code is not going to fail, rather it will throw UNDEFINED. JavaScript takes the declaration and hoists it on top of the script. Here is a good note from Programiz: In hoisting, though it seems that the declaration has moved up in the program, the actual thing that happens is that the function and variable declarations are added to memory during the compile phase. The above code example in compilation looks like this:

const hashnode; console.log(hashnode); hashnode = "best blogging platform"


Var vs Let vs Const

1 yj0O5G2RRrYK_d9qkEqQQg.png

Ref: |

What is an Event Loop?

Ever wondered why JavaScript is an asynchronous programming language? Well, because of something called threading and event loop. JavaScript is a single-threaded synchronous language. JavaScript makes us think it is a multi-threaded language because of the event loop. JavaScript being single-threaded can only handle one thing at a time. Every function call is pushed into the call stack and once it is finished, it is popped from the stack.

But what happens when you have a large number of calls? It keeps stacking them and your calls need to wait before the one above them is finished executing. Result? Your application slows down. Sometimes it takes long enough for the browser to warn that the page is unresponsive. To avoid this and let them execute in an asynchronous manner, we have the event loop.

The implementation usually looks like this:

while (queue.waitForMessage()) {

queue.waitForMessage() waits synchronously for a message to arrive (if one is not already available and waiting to be handled). An event loop is something that pulls stuff out of the queue and places it onto the function execution stack whenever the function stack becomes empty.


Ref: |

setTimeout vs Promise precedence

setTimeout delays the execution of the code block by a specific time duration. Promises are an interface to allow async execution of code. A promise allows code to continue executing while you wait for another action to complete. Usually, this is a network call. Promises executed first and then setTimeout.


setTimeout vs setInterval

setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.

List few ES6 Features

  • Let

  • Promises

  • Arrow Functions

  • Rest Operator

  • Spread Operator

  • Template String

That is it for this one, make sure to check out the second part. And you can follow me on Twitter🐦.

Did you find this article valuable?

Support Amay Jain by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this