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.
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:
Use findIndex when you need a condition:
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.
This is one of the main reasons findIndex exists.
Related Methods You Might Actually Want
Sometimes the correct answer is not findIndex but a neighboring method:
- '
findreturns the matching element itself.' - '
somereturnstrueorfalseif any element matches.' - '
filterreturns all matching elements.' - '
findLastIndexreturns the last matching index in newer JavaScript runtimes.'
Examples:
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:
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
indexOfvariant that accepts a predicate callback. - Use
findIndexwhen you want the index of the first element matching a condition. - Use
indexOfonly for direct value equality checks. - For arrays of objects,
findIndexis usually the correct tool. - Consider
find,some,filter, orfindLastIndexwhen the desired result is not actually an index.
Related reading
- Does JavaScript spawn threads for non-blocking AJAX?
- Does Jquery append behave asynchronously?
- Does node.js preserve asynchronous execution order?
- Does react-native support Multithreading and Background threading or Parallel Execution? How can we do that?
- Download file from url and upload it to AWS S3 without saving - node.js
- DynamoDB get item TypeScript hell
- DynamoDB get item TypeScript hell
- ECMAScript 6 arrow function that returns an object
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.