Ad space (banner)
🟨JavaScript Lessons
Lesson 23 / 69

Searching Arrays and Collections (includes and find)

In this lesson you'll learn to choose between array's .includes() and .find() so you can search data appropriately for your purpose. This is for people searching "JavaScript includes vs find" or "JavaScript array search."

.includes(value) returns true/false for whether an array contains a given value. .find(condition) returns the first matching element itself. The basic idea: use includes if you only want to know "is this product in the list," and use find if you want "the detailed information for that product."

The sample code checks whether "orange" is in the array with fruits.includes("orange"), and searches for the first element starting with "g" using fruits.find((f) => f.startsWith("g")). Also remember that .find() returns undefined if no element matches the condition.

A common beginner stumbling block is that .find() only returns "the first match found." Even if multiple elements match the condition, the second and later ones are ignored. If you want to get all of them, you need to use .filter(), which you learned earlier — understanding the difference between find and filter is important.

In real development, includes and find are extremely commonly used for basic data-search operations — finding a single matching user from an array by ID, or checking whether data satisfying a certain condition exists at all. Learn to pick the right method for the job.

There's also a similar method called .findIndex(), which returns not the element itself but "what position it's at." When you need positional information — like when you want to modify an element — you'll reach for findIndex.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)