JavaScript Array find()
Syntax & Examples
Array.find() method
The find() method of the Array class in JavaScript returns the value of the first element in the array that satisfies the provided testing function. If no element satisfies the testing function, undefined is returned.
Syntax of Array.find()
There are 2 variations for the syntax of Array.find() method. They are:
find(callbackFn)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackFn | required | A function to test each element in the array. It takes three arguments: element, index, and array. |
This method returns the value of the first element in the array that satisfies the provided testing function.
Returns value of type Any
.
find(callbackFn, thisArg)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackFn | required | A function to test each element in the array. It takes three arguments: element, index, and array. |
thisArg | optional | A value to use as this when executing callbackFn. |
This method returns the value of the first element in the array that satisfies the provided testing function, using thisArg as the value of this inside callbackFn.
Returns value of type Any
.
✐ Examples
1 Using find() method to find the first even number
In JavaScript, we can use the find() method to find the first even number in an array.
For example,
- We define an array variable arr with elements [1, 3, 7, 8, 10].
- We define a callback function isEven to check if each element is an even number.
- We use the find() method with isEven to find the first even number in the array.
- The result is stored in the variable firstEven.
- We log firstEven to the console using console.log() method to see the found element.
JavaScript Program
const arr = [1, 3, 7, 8, 10];
const isEven = element => element % 2 === 0;
const firstEven = arr.find(isEven);
console.log(firstEven);
Output
8
2 Using find() method to find the first string with length greater than 3
We can use the find() method to find the first string with length greater than 3 in an array.
For example,
- We define an array variable strArr with elements ['a', 'bc', 'def', 'ghij'].
- We define a callback function isLengthGreaterThanThree to check if the length of each string is greater than 3.
- We use the find() method with isLengthGreaterThanThree to find the first string with length greater than 3 in the array.
- The result is stored in the variable longString.
- We log longString to the console using console.log() method to see the found element.
JavaScript Program
const strArr = ['a', 'bc', 'def', 'ghij'];
const isLengthGreaterThanThree = element => element.length > 3;
const longString = strArr.find(isLengthGreaterThanThree);
console.log(longString);
Output
ghij
3 Using find() method with a thisArg
We can use the find() method with a thisArg to find an element in an array using a specific context.
For example,
- We define an array variable arr with elements [5, 12, 8, 130, 44].
- We define an object context with a threshold property set to 10.
- We define a callback function isGreaterThanThreshold to check if each element is greater than the threshold property of the context object.
- We use the find() method with isGreaterThanThreshold and context as thisArg to find the first element greater than the threshold.
- The result is stored in the variable foundElement.
- We log foundElement to the console using console.log() method to see the found element.
JavaScript Program
const arr = [5, 12, 8, 130, 44];
const context = { threshold: 10 };
function isGreaterThanThreshold(element) {
return element > this.threshold;
}
const foundElement = arr.find(isGreaterThanThreshold, context);
console.log(foundElement);
Output
12
Summary
In this JavaScript tutorial, we learned about find() method of Array: the syntax and few working examples with output and detailed explanation for each example.