On this page
Polyfill: includes and indexOf
Last reviewed 5 Oct 2026
Problem
Implement myIncludes and myIndexOf on Array.prototype, matching the built-ins:
[1, 2, 3].myIncludes(2); // true[1, 2, 3].myIndexOf(2); // 1[1, 2, 3].myIndexOf(4); // -1[1, 2, 3, 2].myIndexOf(2, 2); // 3 (start searching at index 2)[1, 2, 3].myIncludes(1, -2); // false (start two from the end)[NaN].myIncludes(NaN); // true[NaN].myIndexOf(NaN); // -1Clarifying questions
- What is returned?
includesreturns a boolean.indexOfreturns the first matching index, or-1. - How are values compared?
indexOfuses strict equality (===).includesuses SameValueZero, which is===except thatNaNequalsNaN. - Is there a second argument? Yes,
fromIndex, the index to start searching at. It is optional and defaults to 0. - What does a negative
fromIndexmean? Count back from the end:-1starts at the last element. If it reaches past the start, search the whole array. - How are holes in sparse arrays treated?
indexOfskips them.includesreads them asundefined.
Approach
Both methods are the same loop: work out the start index, walk to the end, and return on the first match. They differ in exactly three places.
includes | indexOf | |
|---|---|---|
| Comparison | SameValueZero | strict === |
[NaN] searched for NaN | true | -1 |
| Holes | read as undefined | skipped |
| Returns | true / false | index / -1 |
The start index is shared, so write it once as a helper:
- Convert
fromIndexto an integer.undefinedandNaNbecome 0. - If it is negative, add
length. If it is still negative, use 0. - If it is
lengthor more, the loop never runs and nothing is found.
Step-by-step build
Step 1 — myIndexOf
Array.prototype.myIndexOf = function (target) { for (let i = 0; i < this.length; i++) { if (i in this && this[i] === target) return i; } return -1;};Step 2 — myIncludes
=== says NaN !== NaN, so add one extra check. NaN is the only value that is not equal to itself, which makes x !== x a test for it.
Array.prototype.myIncludes = function (target) { for (let i = 0; i < this.length; i++) { const value = this[i]; if (value === target || (value !== value && target !== target)) return true; } return false;};There is no i in this check, so a hole is read as undefined and [1, , 3].myIncludes(undefined) is true.
Step 3 — fromIndex
function toStartIndex(fromIndex, len) { const n = Math.trunc(Number(fromIndex)) || 0; // undefined and NaN become 0 return n >= 0 ? n : Math.max(len + n, 0); // negative counts from the end}Both loops then start at toStartIndex(fromIndex, len) instead of 0. A start index of len or more needs no special case: the loop condition i < len is false straight away.
Final code
function toStartIndex(fromIndex, len) { const n = Math.trunc(Number(fromIndex)) || 0; return n >= 0 ? n : Math.max(len + n, 0);}
Array.prototype.myIndexOf = function (target, fromIndex) { const len = this.length; for (let i = toStartIndex(fromIndex, len); i < len; i++) { if (i in this && this[i] === target) return i; } return -1;};
Array.prototype.myIncludes = function (target, fromIndex) { const len = this.length; for (let i = toStartIndex(fromIndex, len); i < len; i++) { const value = this[i]; if (value === target || (value !== value && target !== target)) return true; } return false;};Edge cases
NaN:[NaN].myIncludes(NaN)istrue,[NaN].myIndexOf(NaN)is-1.+0and-0are equal for both methods:[-0].myIncludes(0)istrueand[-0].myIndexOf(0)is0.- Holes:
[1, , 3].myIncludes(undefined)istrue, but[1, , 3].myIndexOf(undefined)is-1. A realundefinedelement is found by both. - Objects and arrays are compared by reference:
[{ a: 1 }].myIncludes({ a: 1 })isfalse. - No type coercion:
[1, 2].myIncludes('1')isfalse. fromIndexat or pastlengthfinds nothing. A negative one that reaches past the start (-100on a 3-element array) searches everything.- An empty array returns
falseand-1.
Follow-ups
- Why does
includesexist whenindexOf(x) !== -1works? It reads better, and it findsNaN, whichindexOfcannot. - Is SameValueZero the same as
Object.is? No.Object.is(0, -0)isfalse, while SameValueZero treats them as equal.SetandMapkeys use SameValueZero too. - Implement
lastIndexOf. Walk from the end down to 0, still with===and still skipping holes. ItsfromIndexdefaults tolength - 1and is the index to start searching backwards from. - How would you find an object by its contents?
includesandindexOfcannot. UsesomeorfindIndexwith a callback — see find, findIndex, some and every. - What is the time complexity? O(n) for both. For many lookups against the same data, build a
Setonce and usehas, which is O(1) on average.
Common mistakes
- Using
===inincludes, soNaNis never found. - Using
Object.isinincludes, which wrongly treats0and-0as different. - Skipping holes in
includes, or not skipping them inindexOf. - Using
==, which makes[1].myIncludes('1')true. - Ignoring
fromIndex, or treating a negative one as 0 instead of counting from the end. - Returning the index from
includes: index 0 is falsy, soif (arr.myIncludes(x))would fail for the first element.
Related
- Concepts: comparison operators (
==versus===) in the JavaScript track. - Previous: find, findIndex, some and every.
- Next: Array.flat.