Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript Array find() Method

  • 22 Aug, 2025
  • Com 1
JavaScript Array find() Method

JavaScript array find() is a built-in function designed to search through an array and return the first element that satisfies a provided testing function (callback). If it does not satisfy any condition, it returns undefined.

Searching an element in JavaScript Array

let main_array = [18, 21, 15, 10, 9];

let searched_element = main_array.find(element => element > 19);

console.log(searched_element);

// Output: 21

In this code, we are searching for an element whose value is greater than 19.

The search iteration stops at the first match, making it efficient for large arrays where the match is early.

Syntax

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

Parameters

Argument Description
function(required) It is a function that is executed on each element of the input array until a match is found.
element(required) It is the current element of the array that is being processed.
index(Optional) It represents the index of the current array.
arr(Optional) It is an array on which find() was called.
thisValue(Optional)

It represents an object to use as “this” inside the callback function.

No element is found

If complete traversal occurs, but no match is found, it will return undefined as output.

Array find() returns undefined in JavaScript

let data = [20, 18, 15, 10, 9];

let output = data.find(element => element > 40);

console.log(output);

// Output: undefined

Since there are no elements greater than 40, it returns undefined.

Finding an object by property

The best way to search for a specific object from an array of objects is to compare the value of the object using its key.

Finding an Object by Property in JavaScript

const fruits = [
    { name: 'apples', quantity: 2 },
    { name: 'bananas', quantity: 0 },
    { name: 'cherries', quantity: 5 }
];

const apple_fruit = fruits.find(fruit => fruit.name === 'apples');

console.log(apple_fruit);

// Output: { name: 'apples', quantity: 2 }

Since the apple fruit exists in the array, it returns that object.

Using the “index” parameterVisual Representation of Using the index parameter

Here’s how you can use the index parameter:

let fruits = [
    { name: 'apples', quantity: 2 },
    { name: 'bananas', quantity: 0 },
    { name: 'cherries', quantity: 5 }
];

let basedOnQuantity = fruits.find((tree, i) => { if (tree.quantity > 3 && i !== 0) return true; });

console.log(basedOnQuantity);

// Output: { name: 'cherries', quantity: 5 }

Using thisArg for Context

function isEven(element) {
    return element % this === 0;
}
const numbers = [1, 2, 3, 4, 5];

const evenDivisibleByTwo = numbers.find(isEven, 2);

console.log(evenDivisibleByTwo);

// Output: 2

In this code, thisArg sets this to 2 in the callback. Arrow functions ignore thisArg (e.g., num => num % this === 0 would fail as this is undefined).

Empty array

If the array is empty, the .find() method returns undefined because there is no element to check.

const empty = [];

const result = empty.find(x => x > 0);

console.log(result);

// Output: undefined

Sparse arrays

A sparse array contains empty slots. The find() method skips empty slots (no callback invocation for them).

const sparse = [, , 3, , 5];

const firstOdd = sparse.find(num => num % 2 !== 0);

console.log(firstOdd);

// Output: 3

In this code, even if the input array’s length is 5, there are only indices 2 and 4 are processed.

That’s all!

Post Views: 19
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Split an Integer into a List of Digits in Python
JavaScript Array pop(): Removing the Last Element

1 Comment

  1. indra

    August 13, 2019 at 2:44 am

    it`s a great tutorial thanks

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend