Implement Array.includes

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.includes

Approach

Linear scan from the fromIndex position (defaulting to 0), using strict equality (===) to compare each element with the search value. Handle negative fromIndex by adding it to the array length. Return true on the first match or false after exhausting the array.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Array Polyfill

Why It Works

Includes provides a boolean membership test that is more readable than indexOf !== -1. The fromIndex parameter enables searching from a specific position, useful for finding duplicates after a known index.

Updated Feb 2026