100 most asked JavaScript Interview Questions and Answers — Part 2

JavaScript Interview Questions and Answers Series

Questions

22. How to remove duplicates from an array?

let language = ['JavaScript', 'Dart', 'Kotlin', 'Java', 'Swift', 'Dart']
function unique_array(arr) {
let unique_array = arr.filter(function (elem, index, self) {
return index == self.indexOf(elem);
});
return unique_array
}
console.log(unique_array(language));

// Logs [ 'JavaScript', 'Dart', 'Kotlin', 'Java', 'Swift' ]
let language = ['JavaScript', 'Dart', 'Kotlin', 'Java', 'Swift', 'Dart', 'JavaScript'];function dups_array(language) {
let unique = {};
langugae.forEach(function (i) {
if(!unique[i]) {
unique[i] = true;
}
});
return Object.keys(unique);
}
console.log(dups_array(language));
const set = new Set (['JavaScript', 'Dart', 'Kotlin', 'Java', 'Swift', 'Dart']);function uniquearray() {   let unique_array = Array.from(set);   return unique_array;}console.log(uniquearray());

23. How to check if a value is an Array?

console.log(Array.isArray(5));  //logs false
console.log(Array.isArray("")); //logs false
console.log(Array.isArray()); //logs false
console.log(Array.isArray(null)); //logs false
console.log(Array.isArray({ length: 5 })); //logs false
console.log(Array.isArray([])); //logs true
function isArray(value){
return Object.prototype.toString.call(value) === "[object Array]"
}

24. Implement the Array.prototype.map() method.

let newArray = arr.map(callback(currentValue[, index[, array]]) {
// return element for newArray, after executing something
}[, thisArg]);
function map(arr, mapCallback) {
// Check if the parameters passed are right.
if (!Array.isArray(arr) || !arr.length || typeof mapCallback !== 'function') {
return [];
}
else {
let result = [];
// Avoid mutating the original array.
for (let i = 0, len = arr.length; i < len; i++) {
result.push(mapCallback(arr[i], i, arr));
// push the result of the mapCallback in the 'result' array
}
return result; // return the result array
}
}

25. Implement the Array.prototype.filter() method.

let newArray = arr.filter(callback(currentValue[, index[, array]]) {
// return element for newArray, if true
}[, thisArg]);
function filter(arr, filterCallback) {
// Check if the parameters passed are right.
if (!Array.isArray(arr) || !arr.length || typeof filterCallback !== 'function') {
return [];
}
else {
let result = [];
// Avoid mutating the original array.
for (let i = 0, len = arr.length; i < len; i++) {
// check if the return value of the filterCallback is true or "truthy"
if (filterCallback(arr[i], i, arr)) {
// push the current item in the 'result' array if the condition is true
result.push(arr[i]);
}
}
return result; // return the result array
}
}

26. Implement the Array.prototype.reduce() method.

arr.reduce(callback( accumulator, currentValue, [, index[, array]] )[, initialValue])
function reduce(arr, reduceCallback, initialValue) {
// Check if the parameters passed are right.
if (!Array.isArray(arr) || !arr.length || typeof reduceCallback !== 'function'){
return [];
}
else {
// If no initialValue has been passed to the function we're gonna use the
let hasInitialValue = initialValue !== undefined;
let value = hasInitialValue ? initialValue : arr[0];
// first array item as the initialValue, Start looping at index 1 if there is no
// initialValue has been passed to the function else we start at 0 if there is an initialValue.
for (let i = hasInitialValue ? 0 : 1, len = arr.length; i < len; i++) {
// Then for every iteration we assign the result of the reduceCallback to the variable value.
value = reduceCallback(value, arr[i], i, arr);
}
return value;
}
}

27. What is a name function in JavaScript?

function named() {
// write code here
}

variable and pass it as an argument to another function?

let show = function () {
console.log('Anonymous function');
};
show();

29. What is the arguments object?

30. Can you convert arguments object into an array?

function one() {
return Array.prototype.slice.call(arguments);
}

31. Does arguments object work on ES6 arrow functions?

function one() {
return arguments;
}
const two = function () {
return arguments;
}
const three = function three() {
return arguments;
}
const four = () => arguments;
four(); // Throws an error - arguments is not defined
const four = (...args) => args;

32. How to create an object without a prototype?

const o1 = {};
console.log(o1.toString());
// logs [object Object] get this method to the Object.prototype

const o2 = Object.create(null);
// the first parameter is the prototype of the object "o2" which in this case will be null specifying we don't want any prototype
console.log(o2.toString());
// throws an error o2.toString is not a function

33. What are the scopes of a variable in JavaScript?

34. What is the purpose of this in JavaScript?

35. What is Callback in JavaScript?

36. How does typeOf Operator work?

37. Explain equality in JavaScript.

var a = "42";
var b = 42;
a == b; // true
a === b; // false

38. What is the difference between == and ===?

1 == "1"; // true
1 == [1]; // true
1 == true; // true
0 == ""; // true
0 == "0"; // true
0 == false; // true

39. What is ECMAScript?

40. What are the new features in ES6 or ECMAScript 2015?

41. What does use strict do?

function returnA() {
"use strict";
a = 3245;
return a;
}
"use strict";
var NaN = NaN;
var undefined = undefined;
var Infinity = "and beyond";
"use strict";
const obj = {};
Object.defineProperty(obj, 'x', {
value : '1'
});

delete obj.x;
"use strict";

function someFunc(a, b, b, c){

}
"use strict";

eval("var x = 1;");
console.log(x); //Throws a Reference Error x is not defined
"use strict";

function showMeThis(){
return this;
}
showMeThis(); //returns undefined

Polyglot Fullstack Developer