By the end of this lesson, you will be able to search JavaScript arrays with indexOf, includes, and find to get an index, a boolean, or the matching item.
What it is
Array search means asking an array to locate one or more items that match a condition. Think of an array as a row of boxes: indexOf tells you which box number contains a value, includes tells you whether any box contains it, and find hands you the first item that passes a test.
Related terms include index (zero-based position), predicate (a function that returns true or false), and callback (a function passed to another function).
Why it matters
- Check whether a user has already selected an option.
- Find a product, user, or record by id in a list.
- Validate input against an allowed list.
- Remove duplicates or detect missing values.
- Build filters, search boxes, and conditional UI logic.
Syntax or steps
Use the smallest pattern that matches your goal:
array.indexOf(value)returns the first index ofvalue, or-1if not found.array.includes(value)returnstrueorfalse.array.find(callback)returns the first element for which the callback returnstrue, orundefinedif none match.
For find, the callback receives each element, its index, and the array: (item, index, array) => condition.
Example
const numbers = [1, 2, 3, 4, 5];
const users = [
{ id: 101, name: "Ada" },
{ id: 102, name: "Grace" },
{ id: 103, name: "Alan" }
];
console.log(numbers.indexOf(2)); // 1
console.log(numbers.includes(3)); // true
console.log(numbers.includes(99)); // false
const foundUser = users.find(function (user) {
return user.id === 102;
});
console.log(foundUser); // { id: 102, name: "Grace" }
console.log(users.find(user => user.name.startsWith("A"))); // { id: 103, name: "Alan" }
Part by part: numbers.indexOf(2) searches the primitive array and returns index 1. numbers.includes(3) returns a boolean. users.find(...) checks each object and returns the first object whose id is 102. The arrow function user => user.name.startsWith("A") is a compact predicate that matches names beginning with A.
Common mistakes
- Using
indexOforincludeson objects and expecting property matching. They compare references, so{ id: 1 } === { id: 1 }is false. - Assuming
findreturns an index. It returns the element; usefindIndexif you need the position. - Forgetting that
indexOfreturns-1, notfalse. Check withif (index !== -1). - Writing a callback that accidentally returns
undefinedinstead of a boolean, such as using=instead of===.
When to use it
Choose the method based on what you need back.
| Goal | Use | Best for |
|---|---|---|
| Need position of a primitive value | indexOf | Strings, numbers, booleans, exact references |
| Need yes/no existence | includes | Validation, membership checks |
| Need the matching item or a condition | find | Objects, computed checks, partial matches |
Use indexOf when you need to splice, highlight, or compare positions. Use includes when you only care whether something exists. Use find when the search depends on properties or logic.
Practice
Guided exercise: Given const scores = [82, 91, 75, 99];, use find to get the first score greater than 90.
const scores = [82, 91, 75, 99];
const highScore = scores.find(score => score > 90);
console.log(highScore); // 91
Challenge: Given const products = [{ name: "Book", price: 12 }, { name: "Lamp", price: 45 }];, find the first product with a price above 20. Hint: use products.find(product => product.price > 20).
Quick check
Question: What does array.find() return when no element matches?
Answer: It returns undefined.
Summary
Array search methods turn “look through this list” into clear, reusable code. Use indexOf for positions, includes for existence, and find for conditions or objects. The right choice depends on whether you need an index, a boolean, or the item itself.