100 Most Frequently Asked JavaScript Interview Questions – Part 3

  • Question 21. How do I empty an array in JavaScript?
  • Question 22. How do I remove duplicates from an array?
  • Question 23. How do I check if a value is an array?
  • Question 24. How do I implement the array.prototype.map () method
  • Question 25. How to implement the array.prototype.filter () method
  • Question 26. How to implement the array.prototype.reduce () method
  • Question 27. What are name functions in JavaScript?
  • Question 28. Can an anonymous function be assigned to a variable and passed as an argument to another function?
  • Question 29. What is a arguments object?
  • Question 30. Can a parameter object be converted to an array?
  • The related content

Question 21. How do I empty an array in JavaScript?

A:

There are four ways to empty an array in JavaScript

  • By allocating an empty array:
var array1 = [1.22.24.46];
array1 = [ ];
Copy the code
  • By assigning the array length to 0:
var array1 = [1.22.24.46];
array1.length=0;
Copy the code
  • By popping the elements of the array:
var array1 = [1.22.24.46];
while(array1.length > 0) {
array1.pop();
}
Copy the code
  • By using the concatenated array function:
var array1 = [1.22.24.46];
array1.splice(0, array1.length)
Copy the code

Question 22. How do I remove duplicates from an array?

A:

There are several ways to remove duplicates from arrays, but let me show you one of the most popular.

  • Use filters – By applying a filter to a JavaScript array, you can remove duplicates from it. To invoke thefilter()Method, which takes three arguments. They are arraysself, current elementelemAnd index of the current elementindex.
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' ]
Copy the code

Question 23. How do I check if a value is an array?

A:

  • We can check if the value isArray using the array.isarray () method available on the Array global object.
  • It returns true if the argument passed to it is an array, false otherwise.
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
Copy the code
  • If your environment does not support this approach, you can implement it using Polyfill.
function isArray(value){
   return Object.prototype.toString.call(value) === "[object Array]"
}
Copy the code

Question 24. How do I implement the array.prototype.map () method

A:

As the MDN description of the array.prototype.map method, the map() method creates a new Array that results in a call to the provided function on each element in the call Array.

  • map()The syntax of the method is
let newArray = arr.map(callback(currentValue[, index[, array]]) {
  // return element for newArray, after executing something
}[, thisArg]);
Copy the code
  • This is the implementation of it
function map(arr, mapCallback) {
  // Check if the parameters passed are right.
  if (!Array.isArray(arr) || ! arr.length ||typeofmapCallback ! = ='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}}Copy the code

Question 25. How to implement the array.prototype.filter () method

A:

As the MDN description of the array.prototype.filter method, the filter() method creates a new Array containing all the elements that passed the tests implemented by the provided function.

  • Grammar is
let newArray = arr.filter(callback(currentValue[, index[, array]]) {
  // return element for newArray, if true
}[, thisArg]);
Copy the code
  • Implementation is
function filter(arr, filterCallback) {
  // Check if the parameters passed are right.
  if (!Array.isArray(arr) || ! arr.length ||typeoffilterCallback ! = ='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 trueresult.push(arr[i]); }}return result; // return the result array}}Copy the code

Question 26. How to implement the array.prototype.reduce () method

A:

  • The reduce() method performs the Reducer function (provided by you) on each element of the array to produce a single output value.
  • The Reducer function takes four parameters:

Accumulator, current value, current index, source array

  • Grammar is
arr.reduce(callback( accumulator, currentValue, [, index[, array]] )[, initialValue])
Copy the code
  • perform
function reduce(arr, reduceCallback, initialValue) {
  // Check if the parameters passed are right.
  if (!Array.isArray(arr) || ! arr.length ||typeofreduceCallback ! = ='function') {return [];
  }
  else {
    // If no initialValue has been passed to the function we're gonna use the
    lethasInitialValue = 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);
    }
    returnvalue; }}Copy the code

Question 27. What are name functions in JavaScript?

A:

Named functions declare their names immediately after they are defined. It can be defined as:

function named() {
   // write code here
}
Copy the code

Question 28. Can an anonymous function be assigned to a variable and passed as an argument to another function?

A:

  • Yes! Anonymous functions can be assigned to variables.
  • You can also pass it as an argument to another function.

Example is

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

Question 29. What is a arguments object?

A:

  • A parameter object is a collection of parameter values passed in a function.
  • This is an array-like object, and because it has the Length property, we can access each value using the Array index symbol parameter [1]
  • But it doesn’t have a built-in way in the array to do every simplification, filter and map.
  • It helps us understand the number of arguments passed in the function.

Question 30. Can a parameter object be converted to an array?

A:

  • Yes, we can convert arguments objects to arrays using array.prototype. slice.
function one() {
   return Array.prototype.slice.call(arguments);
}
Copy the code
  • But, if need beautomaticallyExecute a function where the function is given without having to call it againanonymous functionsYou can use it.

Thank you for reading this blog post and I hope it was helpful. I’ll be updating part 4-10 of this series soon, it should be tomorrow, and I’ll keep Posting at least one post a day, follow me, or ❤ or 📑 bookmark this post, and I’ll put a link to it at the end of this post.

Save or long press to identify the author of the public accountWhat do you want biu to order



I will continue to update similar free fun H5 games, Java games, front-end basic knowledge, fun, practical projects and software, and so on

And finally, don’t forget ❤ or 📑

The related content

100 Most Asked JavaScript Interview Questions – Part 1 (1-10) 100 Most Asked JavaScript Interview Questions – Part 2 (11-20)