Table of contents
1.
Introduction
2.
Description  
3.
Syntax
4.
Parameters
5.
Return Value
6.
Different Examples of find() Method
6.1.
Example 1: Finding the First Even Number
6.2.
Example 2: Searching for a Specific Object in an Array
6.3.
Example 3: Using find() with a Custom Context
6.4.
Example 4: Finding Negative Numbers
7.
Supported Browsers
7.1.
Note:
8.
Frequently Asked Questions
8.1.
What happens if no element satisfies the condition in the find() method?
8.2.
Can find() be used on non-array objects?
8.3.
How is find() different from filter()?
9.
Conclusion
Last Updated: Jan 8, 2025
Easy

JavaScript Array find() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript provides various methods to work with arrays efficiently, and the find() method is one of them. This method is used to locate the first element in an array that meets a specified condition. It is incredibly useful when you want to search for an element based on custom logic. 

JavaScript Array find Method

In this article, we will cover the syntax, parameters, return value, and practical examples of the find() method. 

Description  

The `find()` method in JavaScript is a built-in function that allows you to search through an array & return the first element that satisfies a given condition. Unlike other array methods like `filter()` or `map()`, `find()` stops searching as soon as it finds the first matching element. This makes it efficient for scenarios where you only need one result.  

The method takes a callback function as its argument. This callback function is executed for each element in the array until it finds a match. If a match is found, `find()` returns that element. If no match is found, it returns `undefined`.  

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

  1. 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.
       
  2. 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.

Live masterclass