4 methods to search through arrays in JavaScript

Before ECMA6, we have to search through arrays using for loops. Now we have specific methods to search through arrays.

Methods to search through arrays:
  • Array.includes()
  • Array.indexOf()
  • Array.find()
  • Array.filter()

Array.includes():

               It returns a true if a value exists in an array otherwise false

Syntax:
includes(searchElement)

includes(searchElement, fromIndex)

fromIndex is optional. Default is 0. It determines from which index to begin comparison

const arr=[2,4,6,8]

console.log(arr.includes(6))

console.log(arr.includes(10))
Output:

  True

False

In the above example when we search for the element 6 from index 3 it will return false. As the element 6 is in index 2

const arr=[2,4,6,8]

console.log(arr.includes(6,3))
Output:

False

Array.indexOf():

               This method returns the first index at which a given element can be found and -1 if it is not there. It starts at a specified index and search from left to right. By default, start from first element and ends at last element.

Syntax is similar to includes()

const indexOfElement = array.indexOf(element, fromIndex)

element – element you are searching

fromIndex – position or index from where you want to start the search

Example:

const emp=["Anna","Tom","Sam","Henry","Jack"]
let index = emp.indexOf("Henry", 2);
console.log(index)
Output:

3

Array.find():

This method is used to find the first element that meets the certain conditions. If elements do not satisfy the condition, it returns undefined. It is like filter method takes callback as an argument and returns the first element that satisfies the callback condition.

Syntax:
let element = array.find(callback);

callback is the function which is called on each element of the array. This has following parameters.

element- current element being processed.

index- index of current element

array- the array find() is called on.

find() is an iterative method.

Example:
const arr = [10, 20, 45, 120, 99];
const found = arr.find(element => element > 100);
console.log(found);
Output:

120

Array.filter()

This method will return a shallow copy of elements in an array that satisfies the given condition.

Syntax:
let newArray = array.filter(callback);

newArray is the array created with elements that satisfy the conditions.

array- array on which filter method is used.

Callback- function that is applied to each element of the array.

If no element matches the condition then an empty array is returned.

When we want to return only one element that matches the condition then we use find() method.

Example:
const arr = [10, 20, 108, 120, 99];
const found = arr.filter(element => element > 100);
console.log(found);
Output:

[108,120]

Points to remember:
  • includes() method is used to check whether a particular element present in an array. It returns a Boolean value TRUE or FALSE indicating the element is present or not.
  • indexOf() method is used to search for a particular element in an array and return the index of that element. It returns -1 if element is not there.
  • find() method is used to search for the first element that satisfies the given condition. It searches if atleast one element satisfies the condition and returns undefined if not satisfied.
  • filter() method is used to search for multiple elements and returns all the elements that satisfies the condition in a new array. It returns a empty array if no element satisfies.