Array find() & findIndex() Method

JS find() & findIndex() Method

The `find()` and `findIndex()` methods are array methods in JavaScript that allow you to search for a specific element in an array based on a given condition.

1. The `find()` Method

  • Returns the value of the first element in the array that satisfies the provided testing function. Otherwise, it returns undefined.
  • Syntax: array.find(callbackFunction(element, index, array))
  • Mutable: No

Parameters of the callback function:

  • element: The current element being processed in the array.
  • index (Optional): The index of the current element being processed in the array.
  • array (Optional): The array on which find() was called.

Example:

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

In this example, the find() method searches for the first element in the numbers array that is greater than 5 and returns that element. It stops the search as soon as it finds the first matching element.

2. The `findIndex()` Method

  • Returns the index of the first element in the array that satisfies the provided testing function. If no element is found, it returns -1.
  • Syntax: array.findIndex(callbackFunction(element, index, array))
  • Mutable: No

Parameters of the callback function:

  • element: The current element being processed in the array.
  • index (Optional): The index of the current element being processed in the array.
  • array (Optional): The array on which findIndex() was called.

Example:

const numbers = [1, 4, 7, 10, 15];
const index = numbers.findIndex(number => number > 5);
 
console.log(index); // Output: 2

In this example, the findIndex() method searches for the index of the first element in the numbers array that is greater than 5 and returns that index. It stops the search as soon as it finds the first matching element.

Common Use Cases and Differences:

Both find() and findIndex() are used to search for elements based on a condition, but they have some key differences:

Return Value:

  • find(): Returns the value of the first matching element (the actual element itself).
  • findIndex(): Returns the index of the first matching element (a number representing the index).

Return when No Match Found:

  • find(): Returns undefined if no matching element is found.
  • findIndex(): Returns -1 if no matching element is found.

Stopping Condition:

  • find(): Stops searching as soon as it finds the first matching element.
  • findIndex(): Stops searching as soon as it finds the index of the first matching element.

Mutable or Not:

Both find() and findIndex() are non-mutating methods. They do not modify the original array on which they are called. They only return the result of the search operation, leaving the original array unchanged.

Example with Mutation:

const numbers = [1, 4, 7, 10, 15];
 
// Using find() and findIndex() will not modify the original array
const foundNumber = numbers.find(number => number > 5);
const foundIndex = numbers.findIndex(number => number > 5);
 
console.log(numbers); // Output: [1, 4, 7, 10, 15] (Original array remains unchanged)

Key Takeaways:

  • The `find()` method returns the first element that satisfies the provided testing function, while the `findIndex()` method returns the index of the first element that satisfies the testing function.

  • If no element satisfies the condition, the `find()` method returns `undefined`, and the `findIndex()` method returns `-1`.

  • Both methods have an optional `thisValue` parameter which can be used as `this` when executing the callback function.

  • The callback function provided to `find()` and `findIndex()` should return a boolean value indicating whether the current element satisfies the condition or not.

Comments

Popular posts from this blog

Limitations of JavaScript

'for in' Loop

What is JIT compiler? Is JavaScript compiled or interpreted or both?