Top 10 Most Asked JavaScript Interview Questions With Answers Part 01

Muhammad Umair
8 min readJun 1, 2022

By Muhammad Umair

JavaScript Interview Questions & Answers Part 01 By Muhammad Umair

Today, Google and Facebook use JavaScript to build complex, desktop-like web applications. With the launch of Node.js, It has also become one of the most popular languages for building server-side software. Today, even the web isn’t big enough to contain JavaScript’s versatility. I believe that you are already aware of these facts and this has made you land on this JavaScript Interview Questions article.

So, if you are planning to start your career in JavaScript and you wish to know the skills related to it, now is the right time to dive in, when the technology is in its blossoming state. JavaScript Interview Questions will provide you with in-depth knowledge and help you prepare for your interviews.

Question No 1:

What are the possible ways to create objects in JavaScript

There are many ways to create objects in JavaScript as below

  1. Object constructor:

The simplest way to create an empty object is using the Object constructor. Currently, this approach is not recommended.

var object = new Object();

2. Object’s create method:

he create method of Object creates a new object by passing the prototype object as a parameter

var object = Object.create(null); 

3. Object literal syntax:

The object literal syntax (or object initializer), is a comma-separated set of name-value pairs wrapped in curly braces.

var object = {name: "Umair", age: 22 };
Object literal property values can be of any data type, including array, function, and nested object.

Note: This is the easiest way to create an object

4. Function constructor:

Create any function and apply the new operator to create object instances,

function Person(name) {
this.name = name;
this.age = 22;
}
var object = new Person("Umair");

5. Function constructor with prototype:

This is similar to a function constructor but it uses prototypes for their properties and methods,

function Person() {} Person.prototype.name = "Umair"; var object = new Person();

This is equivalent to an instance created with an object create a method with a function prototype and then calling that function with an instance and parameters as arguments.

function func {};  
new func(x, y, z);

(OR)

// Create a new instance using function prototype.
var newInstance = Object.create(func.prototype)
// Call the function
var result = func.call(newInstance, x, y, z),
// If the result is a non-null object then use it otherwise just use the new instance.
console.log(result && typeof result === 'object' ? result : newInstance);

6. ES6 Class syntax:

ES6 introduces class feature to create the objects

class Person {   constructor(name) {     this.name = name;   } }  var object = new Person("Umair");

7. Singleton pattern:

A Singleton is an object which can only be instantiated one time. Repeated calls to its constructor return the same instance and this way one can ensure that they don’t accidentally create multiple instances.

var object = new (function () {
this.name = "Umair";
})();

Question No 2:

What is a prototype chain?

Prototype chaining is used to build new types of objects based on existing ones. It is similar to inheritance in a class-based language.

The prototype on object instance is available through Object.getPrototypeOf(object) or proto property whereas prototype on constructors function is available through Object.prototype.

Question No 3:

What is the difference between Call, Apply and Bind

The difference between Call, Apply and Bind can be explained below examples,

Call: The call() method invokes a function with a given this value and arguments provided one by one

var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };
function invite(greeting1, greeting2) {
console.log(greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
);
}

invite.call(employee1, "Hello", "How are you?"); // Hello John Rodson, How are you?
invite.call(employee2, "Hello", "How are you?"); // Hello Jimmy Baily, How are you?

Apply: Invokes the function with a given this value and allows you to pass in arguments as an array

var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };

function invite(greeting1, greeting2) {
console.log(
greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
);
}

invite.apply(employee1, ["Hello", "How are you?"]); // Hello John Rodson, How are you?
invite.apply(employee2, ["Hello", "How are you?"]); // Hello Jimmy Baily, How are you?

bind: returns a new function, allowing you to pass any number of arguments

var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };

function invite(greeting1, greeting2) {
console.log(
greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2);
}

var inviteEmployee1 = invite.bind(employee1);
var inviteEmployee2 = invite.bind(employee2);
inviteEmployee1("Hello", "How are you?");
inviteEmployee2("Hello", "How are you?");

Call and apply are pretty interchangeable. Both execute the current function immediately. You need to decide whether it’s easier to send in an array or a comma-separated list of arguments. You can remember by treating Call as for a comma (separated list) and Apply as for Array.

Whereas Bind creates a new function that will have this set to the first parameter passed to bind().

Question No 4:

What are JSON and its common operations?

JSON is a text-based data format following JavaScript object syntax, which was popularized by Douglas Crockford. It is useful when you want to transmit data across a network and it is basically just a text file with an extension of .json, and a MIME type of application/json

Parsing: Converting a string to a native object

JSON.parse(text);

Stringification: converting a native object to a string so it can be transmitted across the network

JSON.stringify(object);

Question No 5:

What is the purpose of the array slice method

The slice() method returns the selected elements in an array as a new array object. It selects the elements starting at the given start argument and ends at the given optional end argument without including the last element. If you omit the second argument then it selects till the end.

Some of the examples of this method are,

let arrayIntegers = [1, 2, 3, 4, 5];
let arrayIntegers1 = arrayIntegers.slice(0, 2); // returns [1,2]
let arrayIntegers2 = arrayIntegers.slice(2, 3); // returns [3]
let arrayIntegers3 = arrayIntegers.slice(4); //returns [5]

Note: The slice method won’t mutate the original array but it returns the subset as a new array.

Question No 6:

What is the purpose of the array splice method?

The splice() method is used either adds/remove items to/from an array, and then return the removed item. The first argument specifies the array position for insertion or deletion whereas the optional second argument indicates the number of elements to be deleted. Each additional argument is added to the array.

Some of the examples of this method are,

let arrayIntegersOriginal1 = [1, 2, 3, 4, 5];
let arrayIntegersOriginal2 = [1, 2, 3, 4, 5];
let arrayIntegersOriginal3 = [1, 2, 3, 4, 5];
let arrayIntegers1 = arrayIntegersOriginal1.splice(0, 2); // returns [1, 2]; original array: [3, 4, 5]
let arrayIntegers2 = arrayIntegersOriginal2.splice(3); // returns [4, 5]; original array: [1, 2, 3]
let arrayIntegers3 = arrayIntegersOriginal3.splice(3, 1, "a", "b", "c"); //returns [4]; original array: [1, 2, 3, "a", "b", "c", 5]

Note: The splice method modifies the original array and returns the deleted array.

Question No 7:

What is the difference between slice and splice

Some of the major differences in a tabular form

Slice

Doesn’t modify the original array(immutable)

Returns the subset of the original array

Used to pick the elements from array

Splice

Modifies the original array(mutable)

Returns the deleted elements as array

Used to insert or delete elements to/from an array

Question No 8:

How do you compare Objects and Map?

Objects are similar to Maps in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. Due to this reason, Objects have been used as Maps historically. But there are important differences that make using a Map preferable in certain cases.

  1. The keys of an Object are Strings and Symbols, whereas they can be any value for a Map, including functions, objects, and any primitive.
  2. The keys in Map are ordered while keys added to Object are not. Thus, when iterating over it, a Map object returns keys in order of insertion.
  3. You can get the size of a Map easily with the size property, while the number of properties in an Object must be determined manually.
  4. A Map is iterable and can thus be directly iterated, whereas iterating over an Object requires obtaining its keys in some fashion and iterating over them.
  5. An Object has a prototype, so there are default keys in the map that could collide with your keys if you’re not careful. As of ES5, this can be bypassed by using map = Object.create(null), but this is seldom done.
  6. A Map may perform better in scenarios involving frequent addition and removal of key pairs.

Question No 9:

What is the difference between == and === operators

JavaScript provides both strict(===, !==) and type-converting(==, !=) equality comparison. The strict operators take the type of variable into consideration, while non-strict operators make type correction/conversion based upon values of variables. The strict operators follow the below conditions for different types,

  1. Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  2. Two numbers are strictly equal when they are numerically equal. i.e, Having the same number value. There are two special cases in this,
  3. NaN is not equal to anything, including NaN.
  4. Positive and negative zeros are equal to one another.
  5. Two Boolean operands are strictly equal if both are true or both are false.
  6. Two objects are strictly equal if they refer to the same Object.
  7. Null and Undefined types are not equal with ===, but equal with ==. i.e, null===undefined → false but null==undefined → true
  8. Some of the example which covers the above cases,
0 == false   // true
0 === false // false
1 == "1" // true
1 === "1" // false
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
[]==[] or []===[] //false, refer different objects in memory
{}=={} or {}==={} //false, refer different objects in memory

Question No 10:

What are lambda or arrow functions

An arrow function is a shorter syntax for a function expression and does not have its own this, arguments, super, or new.target. These functions are best suited for non-method functions, and they cannot be used as constructors.

With this, we have come to the end of Top 10 Most Asked JavaScript Interview Questions With Answers Part 01. I Hope these JS Interview Questions will help you in your interviews. In case you have attended interviews in the recent past, do paste those interview questions in the comments section and we’ll answer them. You can also comment below if you have any questions in your mind, which you might face in your JavaScript interview.

--

--

Muhammad Umair

MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python