Back to JavaScript Notes
Topic #324

Array Search

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 of value, or -1 if not found.
  • array.includes(value) returns true or false.
  • array.find(callback) returns the first element for which the callback returns true, or undefined if 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 indexOf or includes on objects and expecting property matching. They compare references, so { id: 1 } === { id: 1 } is false.
  • Assuming find returns an index. It returns the element; use findIndex if you need the position.
  • Forgetting that indexOf returns -1, not false. Check with if (index !== -1).
  • Writing a callback that accidentally returns undefined instead of a boolean, such as using = instead of ===.

When to use it

Choose the method based on what you need back.

GoalUseBest for
Need position of a primitive valueindexOfStrings, numbers, booleans, exact references
Need yes/no existenceincludesValidation, membership checks
Need the matching item or a conditionfindObjects, 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Array Search – FAQs

Quick answers about learning Array Search in JavaScript.

This free note from Coding Now Tech Institute explains Array Search in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Array Search, is 100% free with no signup required.
With focused practice, most students grasp Array Search in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now