Implement Array.findIndex

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start•← → to step•? all shortcuts

Solution Guide: Implement Array.findIndex

Approach

Loop through the array and test each element against the callback predicate. Return the index of the first element that passes the test, short-circuiting the iteration. Return -1 if no element satisfies the condition.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Array Polyfill

Why It Works

FindIndex returns a numeric position rather than the element itself, which is useful when you need to know where a match occurs for subsequent splice, slice, or index-based operations.

Updated Mar 2026