Syntax
The syntax of the find() method is simple:
array.find(callback(element[, index[, array]])[, thisArg])
Explanation:
- array: The array on which the find() method is called.
- callback: A function that is executed on each element of the array until a condition is met.
- thisArg (optional): The value to use as this when executing the callback function.
Let’s look at a simple example to understand how this works:
const numbers = [10, 20, 30, 40, 50];
const result = numbers.find(function(number) {
return number > 25;
});
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
30
In this example, the `find()` method checks each number in the `numbers` array. It stops at `30` because it’s the first number greater than `25` & returns it.
Parameters
- callback: A function with the following arguments:
- element: The current element being processed in the array.
- index (optional): The index of the current element.
- array (optional): The array that find() is operating on.
- thisArg: Optional parameter. If provided, it will be used as the value of this inside the callback function.
Return Value
The find() method returns the first element in the array that satisfies the condition specified in the callback function. If no such element is found, it returns undefined.
Different Examples of find() Method
Example 1: Finding the First Even Number
const numbers = [1, 3, 5, 8, 10];
const firstEven = numbers.find(num => num % 2 === 0);
console.log(firstEven);

You can also try this code with Online Javascript Compiler
Run Code
Output:
8
Explanation:
In this example, the find() method searches for the first number that is divisible by 2. It returns 8, as it is the first even number in the array.
Example 2: Searching for a Specific Object in an Array
const students = [
{ id: 1, name: "Alice", age: 20 },
{ id: 2, name: "Bob", age: 22 },
{ id: 3, name: "Charlie", age: 23 }
];
const student = students.find(student => student.id === 2);
console.log(student);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ id: 2, name: "Bob", age: 22 }
Explanation: Here, find() is used to locate the student object with an id of 2. The method returns the entire object that matches the condition.
Example 3: Using find() with a Custom Context
const fruits = ["apple", "banana", "grape", "orange"];
function checkFruit(fruit) {
return this.includes(fruit);
}
const context = ["grape", "orange"];
const result = fruits.find(checkFruit, context);
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
grape
Explanation: In this example, the find() method uses a custom thisArg (the context array) to determine if a fruit exists in the list. The first fruit found in the context array is returned.
Example 4: Finding Negative Numbers
const numbers = [10, 5, -3, 0, 7];
const firstNegative = numbers.find(num => num < 0);
console.log(firstNegative);

You can also try this code with Online Javascript Compiler
Run Code
Output:
-3
Explanation: This example demonstrates how to find the first negative number in an array.
Supported Browsers
The find() method is supported by most modern browsers, including:
- Google Chrome: Version 45 and above
- Mozilla Firefox: Version 25 and above
- Microsoft Edge: All versions
- Safari: Version 7.1 and above
- Opera: Version 32 and above
Note:
If you are working in an environment that does not support find(), you can use a polyfill to achieve similar functionality.
Frequently Asked Questions
What happens if no element satisfies the condition in the find() method?
If no element meets the condition specified in the callback, the find() method returns undefined.
Can find() be used on non-array objects?
No, find() is specifically designed to work with arrays. For non-array objects, you can convert them into arrays first and then use find().
How is find() different from filter()?
The find() method returns only the first element that matches the condition, while filter() returns all matching elements as a new array.
Conclusion
The find() method in JavaScript is a powerful tool for searching through arrays to find the first element that satisfies a given condition. Whether you are working with numbers, strings, or objects, find() makes the search process easy and efficient.
You can also check out our other blogs on Code360.