Most Asked JavaScript Interview Questions - 1
We will see the most asked basic to advanced JavaScript interview questions in this first set.
Photo by Oskar Yildiz on Unsplash
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:
console.log(hashnode);
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
Ref: geeksforgeeks.org/difference-between-var-le.. | rixong.medium.com/var-let-and-const-28ab64a..
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.processNextMessage();
}
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: developer.mozilla.org/en-US/docs/Web/JavaSc.. | dev.to/lydiahallie/javascript-visualized-ev..
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!