Searching and Filtering in JS

Searching and Filtering in javascript means searching in a string/array datatype. And we have few javascript built in methods for both searching and filtering.
Imagine a scenario where we need to build a feature doing both searching and filtering, then below methods comes handy. All the methods can be used for both array or strings in the same way. The methods we will study are filter(), find(), findLast(), findIndex(), indexOf(), lastIndexOf() and includes().

filter(): filter() creates a brand-new array with every element that passes a particular condition. A callback function that should return true or false values for each element is accepted as an argument. The elements that return true are in the new array.
Example:

const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter((number) => number % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]


find(): The first element in the array that satisfies a certain criterion is returned by the find() method. The first element that the callback function returns true for is what is returned. It accepts a callback function.
Example:

const numbers = [1, 2, 3, 4, 5];
const foundNumber = numbers.find((number) => number > 3);
console.log(foundNumber); // Output: 4


findLast(): The first element whose value meets the supplied testing function is returned by the findLast() method, which iterates the array in reverse order. Undefined is returned if no elements pass the testing method.
Example:

// syntax
findLast(callbackFn)
findLast(callbackFn, thisArg)

// example:
const numList = [5, 12, 50, 130, 44];

const found = numList.findLast((element) => element > 48);

console.log(found);
// Expected output: 130


findIndex(): The first entry in the array that satisfies a specific condition is returned by the findIndex() method's index. Once a callback function has been passed, the index of the first element for which it returns true is returned. And if we find the index then we can easily find the element on that index by arr[indx], where arr is the array and indx is the index that we find with this method.
Example:

const numbers = [1, 2, 3, 4, 5];
const foundIndex = numbers.findIndex((number) => number > 3);
console.log(foundIndex); // Output: 3


indexOf(): The index of the first instance of a specified element in an array is returned by the indexOf() method. If the element cannot be found, the method returns -1.
Example:

const fruits = ['apple', 'banana', 'orange'];
const index = fruits.indexOf('banana');
console.log(index); // Output: 1


lastIndexOf(): The lastIndexOf() function returns the index of a specified element's last occurrence in an array. The function returns -1 if the element cannot be found.
Example:

const fruits = ['apple', 'banana', 'orange', 'banana'];
const lastIndex = fruits.lastIndexOf('banana');
console.log(lastIndex); // Output: 3


includes(): TThis method determines whether an array contains a particular element. If the element is found, it returns true; otherwise, it returns false.
Example:

const numbers = [1, 2, 3, 4, 5];
const includesNumber = numbers.includes(3);
console.log(includesNumber); // Output: true