Javascript Common Interview Questions

Before every interview, I like to review those important Javascript concepts which most likely would be asked in a Technical interview.

But this is not history or poetry, in Tech, I’ve learned that there is no one true answer to any question. I mean you don’t have to memorize every single word in a definition to Closure which by the way, is the most asked question in every interview said to me by “Skilled’s interviewer”.

So when you are asked a question, don’t get nervous of not saying the right words. Just say what comes into your mind like you have a computer in front of you with some code, explain what happens when you run it.

Now Let’s talk about the most common questions in an interview in Javascript.

1- Closure:

The closure is a collection of all the variables in scope at the time of creation of the function. The way I will always remember closures is through the backpack analogy. When a function gets created and passed around or returned from another function, it carries a backpack with it. And in the backpack are all the variables that were in scope when the function was declared.

2- Scope

The scope is a policy that manages the accessibility of variables.

Block Scope: A code block in JavaScript defines a scope for variables declared using let and const while var is not block scoped.

Function Scope: A function in JavaScript defines a scope for variables declared using var, let and const.

Lexical Scope: The lexical scope consists of outer scopes determined statically.

Let’s define 2 functions, having the function innerFunc() is nested inside outerFunc().

Try to figure out what inner() will return before you run it, it will give you a clear idea of how both Closure and Lexical Scope work.

3- What is the difference between var, let and const?

Before 2015, var was the only way you can declare a variable, now everyone will tell you, don’t ever use var and this is why:

`const` is a signal that the identifier won’t be reassigned.

`let` is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.

`var` is now the weakest signal available when you define a variable in JavaScript. The variable may or may not be reassigned, and the variable may or may not be used for an entire function, or just for the purpose of a block or loop.

4- How does Async work?

JavaScript is a single-threaded programming language which means only one thing can happen at a time. That is, the JavaScript engine can only process one statement at a time in a single thread.

While the single-threaded languages simplify writing code because you don’t have to worry about the concurrency issues, this also means you can’t perform long operations such as network access without blocking the main thread.

Imagine requesting some data from an API. Depending upon the situation the server might take some time to process the request while blocking the main thread making the web page unresponsive.

That’s where asynchronous JavaScript comes into play. Using asynchronous JavaScript (such as callbacks, promises, and async/await), you can perform long network requests without blocking the main thread.

5- What is a constructor?

A constructor is a function that creates an instance of a class which is typically called an “object”. In JavaScript, a constructor gets called when you declare an object using the new keyword.

The purpose of a constructor is to create an object and set values if there are any object properties present. It’s a neat way to create an object because you do not need to explicitly state what to return as the constructor function, by default, returns the object that gets created within it.

6- Tell me about Arrow Functions ?

Arrow functions — also called “fat arrow” functions, from CoffeeScript (a transcompiled language) — are a more concise syntax for writing function expressions. They utilize a new token, =>, that looks like a fat arrow. Arrow functions are anonymous and change the way this binds in functions.

Arrow functions make our code more concise, and simplify function scoping and the this keyword. By using arrow functions, we avoid having to type the function keyword, return keyword (it’s implicit in arrow functions), and curly brackets.

7- What is a First Class Function ?

In JavaScript, functions are first-class objects, which means they can be:

- stored in a variable, object, or array

- passed as an argument to a function

- returned from a function

8- Tell me about a Promise in Javascript ?

A promise is an object that may produce a single value some time in the future : either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred). … Promise users can attach callbacks to handle the fulfilled value or the reason for rejection.

9- What is Hoisting in Javascript ?

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Inevitably, this means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local

10- Tell me about OOP in Javascript ?

Object oriented programming is based on the idea of using objects to represent data and methods. We use classes as a blueprint to create an object. Classes do not contain any data but it will define what properties and operations can be performed.

The two important principles with OOP in JavaScript are Object Creation patterns (Encapsulation) and Code Reuse patterns (Inheritance). When building applications, you create many objects, and there exist many ways for creating these objects: you can use the ubiquitous object literal pattern

I hope this helps in your coming interviews!!

Mokhtar Ali

Web Developer | World Traveler | Positive Attitude

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store