In this story, we will see about the different methods in JavaScript to search an item in an array.

There are four different methods to search an array in JavaScript. They are as follows:

  • Filter
  • Find
  • Includes
  • IndexOf

From the above different methods, we want to choose the method depends upon our usecase.

Now let’s get deep into the topic.

Array.filter()

Array.filter() method is used to find an element in an array under specific condition. Let’s see an example that print all the elements in an array of number that is greater than 10.

const array = [10, 11, 3, 20, 5];

const greaterThanTen = array.filter(element => element > 10);

console.log(greaterThanTen) //[11, 20]

Syntax:

let newArray = array.filter(callback);

Where

  • newArray - It is the array which is returned by the function.
  • array- It is an array in which the filter method is called.
  • callback- It is a callback function which is applied to all the elements in an array.

#web-development #javascript #nodejs #array-methods

4 Different Methods to Search an Array in JavaScript
14.20 GEEK