Top 10 JavaScript Interview Questions — Part -2

azhar bin Shakil
4 min readOct 17, 2021

1. What is the ‘strict’ mode in JavaScript

Answer: The most important answer here is that use strict is a way to enforce stricter parsing and error handling on your JavaScript code at runtime voluntarily. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions. In general, it is a good practice.

2. How do you check if an object is an array or not?

Answer: In JavaScript, we can check if a variable is an array by using 3 methods, using the isArray method, using the instanceof operator, and using checking the constructor type if it matches an Array object.

Method 1: Using the isArray method

The Array.isArray() method checks whether the passed variable is an Array object.

Syntax:

Array.isArray(variableName)

It returns a true boolean value if the variable is an array and a false one if it is not.

Method 2: Using the instanceof operator
The instanceof operator is used to test whether the prototype property of a constructor appears anywhere in the prototype chain of an object. This can be used to evaluate if the given variable has a prototype of ‘Array’.

Syntax:

variable instanceof Array

The operator returns a true boolean value if the variable is the same as what is specified (here is an Array) and false if it is not.

Method 3: Checking the constructor property of the variable

Another method to check a variable is an array is by checking its a constructor with Array.

Syntax:

variable.constructor === Array

This becomes true if the variable is the same as what is specified (here is an Array) and false if it is not.

3 . Suppose we have an unsorted array of numbers 1 to 100 excluding one number, write a code to find that number.

Answer:

function getMissingNumber(arr){
const n = arr.length+1; //+1 because 1 number is missing
const totalSum = (n*(n+1))/2;
let currentSum = 0;
arr.map(num => currentSum+=num);
return totalSum - currentSum;
}
console.log(getMissingNumber([5,2,4,1]))

4. How do you reverse a String in javaScript?

Answer: If it’s allowed to use build-in methods then I will split the string into an array to use the reverse method of the array, after then I will convert/join the array to string again.

const reverseString = (str) => {
return str.split("").reverse().join("");
}console.log(reverseString("Hello World")); // olleH dlroW

5 . How can you check a string is a Palindrome or not?

Answer: Palindrome in a word that stays as same after reversed. As Example “madam” if we reverse it, it will be still “madam”. We can check this in the program by comparing the original string with that’s reversed form.

const palindrome = (str) => { if(str === str.split("").reverse().join(""))
return true; else
return false;}console.log(palindrome("madam")); // True
console.log(palindrome("moon")); // False

6 . How would you remove duplicate members from an array?

Answer: First of all I will create a new empty array and I will use a loop to traverse all items of the original array In a time of traversing of the array I will make a condition to check if the item is exist in my new array or not, basically I will only check the for the falsy condition, if the item does not exist in my new array then I will push the item in my new array After over of the iteration I will get the new Array fulfilled with unique items.

const removeDulicate = (arr) => {
const newArr = [];
for(let i=0; i < arr.length; i ++) {
let el = arr[i]; if(newArr.indexOf(el) == -1) {
newArr.push(el)
}
} return newArr
}

7 . What is the Generator function?

Answer: Generator functions are a new feature in ES6 that allows a function to generate many values over time by returning an object which can be iterated over to pull values from the function one value at a time.

8 . What is Set in javaScript?

Answer: Set is a Data structure that is newly introduced in ES6. Set objects are collections of unique values. Duplicate values are ignored, as the collection must have all unique values. The values can be primitive types or object references.

9 . What is Map and WeakMap in JavaScript?

Answer: ES6 introduced the new map and objects to JavaScript. They can be used as a key-value store, but unlike normal objects in JavaScript, you can use objects as keys. Keys for normal objects ({}) always have to be strings, and if you try to use an object it will be converted to a string ([object Object]).

10 . What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?

Answer: This is an increasingly common practice, employed by many popular JavaScript libraries. This technique creates a closure around the entire contents of the file which, perhaps most importantly, creates a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries.

--

--