JavaScript
indexOf
lambda
programming
code

Does JavaScript have an indexOflambda or similar?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

JavaScript does not have an indexOf overload that accepts a lambda-style predicate. If you want "the index of the first element that matches this condition", the method you want is findIndex.

That distinction matters because indexOf only checks for equality with a specific value, while findIndex runs a callback on each element until one returns true.

indexOf Versus findIndex

Use indexOf when you already know the exact primitive value:

javascript
const colors = ["red", "green", "blue"];
console.log(colors.indexOf("green")); // 1
console.log(colors.indexOf("purple")); // -1

Use findIndex when you need a condition:

javascript
1const users = [
2  { id: 10, name: "Ana" },
3  { id: 11, name: "Ben" },
4  { id: 12, name: "Cara" },
5];
6
7const index = users.findIndex(user => user.id === 11);
8console.log(index); // 1

That arrow function is the JavaScript equivalent of the "lambda" idea in other languages.

Why indexOf Is Not Enough for Objects

indexOf uses equality, not a predicate. For objects, that means it only finds the exact same object reference, not another object with the same contents.

javascript
1const target = { id: 11 };
2const items = [{ id: 11 }, { id: 12 }];
3
4console.log(items.indexOf(target)); // -1
5console.log(items.findIndex(item => item.id === 11)); // 0

This is one of the main reasons findIndex exists.

Sometimes the correct answer is not findIndex but a neighboring method:

  • 'find returns the matching element itself.'
  • 'some returns true or false if any element matches.'
  • 'filter returns all matching elements.'
  • 'findLastIndex returns the last matching index in newer JavaScript runtimes.'

Examples:

javascript
1const values = [3, 8, 12, 8];
2
3console.log(values.find(n => n > 5));       // 8
4console.log(values.findIndex(n => n > 5));  // 1
5console.log(values.some(n => n > 10));      // true
6console.log(values.filter(n => n > 5));     // [8, 12, 8]

Choosing the right method makes the code clearer than forcing everything into an indexOf mental model.

A Reusable Helper if You Miss the Pattern

If you prefer a more explicit utility, you can wrap findIndex in a helper:

javascript
1function indexWhere(array, predicate) {
2  return array.findIndex(predicate);
3}
4
5const result = indexWhere(
6  ["alpha", "beta", "gamma"],
7  item => item.startsWith("g")
8);
9
10console.log(result); // 2

This is mostly stylistic. Under the hood, findIndex is already the standard API for the job.

One small advantage of a helper is naming. In codebases where teammates come from other languages, a function named indexWhere or firstIndex can communicate intent faster than a raw array method call.

Common Pitfalls

The first mistake is expecting indexOf to work on object contents. It does not compare object fields. It compares references.

Another common issue is forgetting that findIndex returns -1 when nothing matches. If you use the result as an array index without checking, you can introduce subtle bugs.

Be careful with truthy and falsy return values inside the predicate. findIndex expects a condition. Returning a string, number, or object may still work because JavaScript coerces values to booleans, but that makes the code harder to read.

Finally, remember that these methods stop at the first match. If you need all matches, use filter and then derive indices if necessary.

Performance is rarely a reason to prefer one of these methods over another for small arrays. The clearer distinction is semantic: equality lookup versus predicate-based lookup.

Summary

  • JavaScript does not have an indexOf variant that accepts a predicate callback.
  • Use findIndex when you want the index of the first element matching a condition.
  • Use indexOf only for direct value equality checks.
  • For arrays of objects, findIndex is usually the correct tool.
  • Consider find, some, filter, or findLastIndex when the desired result is not actually an index.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.