Javascript - Array find() Method

The find() method returns the value of the first element in an array that pass a test.

Description

The find() method returns the value of the first element in an array that pass a test.

The find() method executes the function once for each element present:

If it finds an array element where the function returns a true value, find() returns the value of that array element and does not check the remaining values.

Otherwise it returns undefined

find() does not run the function for array elements without values.

find() does not change the original array.

Syntax

array.find(function(currentValue, index, arr),thisValue)

Parameter Values

Parameter Require Description
function(currentValue, index, arr) Required. A function to be run for each element in the array.
thisValue Optional. A value to be passed to the function to be used as its "this" value. If this parameter is empty, the value "undefined" will be passed as its "this" value

function(currentValue, index, arr)

Argument Require Description
currentValue Required. The value of the current element
index Optional. The array index of the current element
arr Optional. The array object the current element belongs to

Return

Returns the array element value if any of the elements in the array pass the test, otherwise it returns undefined

Example

Get the value of the first element in the array that has a value of 18 or more:

Demo

var ages = [3, 10, 18, 20];

function checkAdult(age) {
    return age >= 18;
}

console.log( ages.find(checkAdult));

Result

Array find() method iterates through array and returns the first item that matches the callback(element, index, array).

The find() method returns the first value in the array for which the testing function returns true; otherwise undefined is returned.

This method allows you to optionally pass a context binding for this.

The syntax for the find() method looks like this:

arr.find(callback[, thisArg]) 
  • where callback is the testing function that is executed on each value and takes three arguments: element, index and array.
function callback(element, index, array) { 
// returns true or false based on some condition 
} 

Consider the following code snippet:

const inventory = [ 
       {name: 'XMLs', quantity: 2}, 
       {name: 'Screens', quantity: 0}, 
       {name: 'Keyboards', quantity: 5} 
]; 

let result = inventory.find((fruit) => fruit.name === 'XMLs'); 
console.log(result);             // {name: 'XMLs', quantity: 2} 

Here, it shows how we can find the first element that matches the fruit.name as 'XMLs' in the given array.

You can use find() method to get the element at a certain index position, for example:

result = inventory.find((fruit, i) => i > 2); 
console.log(result);             // {name: 'Keyboards', quantity: 5} 

find() method returns undefined if the element is not found in the array:

result = inventory.find((fruit, i) => i > 10); 
console.log(result);             // undefined 

The next argument thisArg lets you optionally pass a context binding for this:

function eligibleToVote(age) { 
    return age >= this.legalAge; 
} 

function foo() { 
       this.legalAge = 18; 
       const result = [10, 12, 23, 26, 32].find(eligibleToVote, this); 
       console.log(result); 
} 

foo();     // 23 

Here, the context of foo is passed as the second argument in the find() method.