On this page
Tracks

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); // -1

Clarifying questions

  • What is returned? includes returns a boolean. indexOf returns the first matching index, or -1.
  • How are values compared? indexOf uses strict equality (===). includes uses SameValueZero, which is === except that NaN equals NaN.
  • Is there a second argument? Yes, fromIndex, the index to start searching at. It is optional and defaults to 0.
  • What does a negative fromIndex mean? Count back from the end: -1 starts at the last element. If it reaches past the start, search the whole array.
  • How are holes in sparse arrays treated? indexOf skips them. includes reads them as undefined.

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.

includesindexOf
ComparisonSameValueZerostrict ===
[NaN] searched for NaNtrue-1
Holesread as undefinedskipped
Returnstrue / falseindex / -1

The start index is shared, so write it once as a helper:

  1. Convert fromIndex to an integer. undefined and NaN become 0.
  2. If it is negative, add length. If it is still negative, use 0.
  3. If it is length or 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) is true, [NaN].myIndexOf(NaN) is -1.
  • +0 and -0 are equal for both methods: [-0].myIncludes(0) is true and [-0].myIndexOf(0) is 0.
  • Holes: [1, , 3].myIncludes(undefined) is true, but [1, , 3].myIndexOf(undefined) is -1. A real undefined element is found by both.
  • Objects and arrays are compared by reference: [{ a: 1 }].myIncludes({ a: 1 }) is false.
  • No type coercion: [1, 2].myIncludes('1') is false.
  • fromIndex at or past length finds nothing. A negative one that reaches past the start (-100 on a 3-element array) searches everything.
  • An empty array returns false and -1.

Follow-ups

  • Why does includes exist when indexOf(x) !== -1 works? It reads better, and it finds NaN, which indexOf cannot.
  • Is SameValueZero the same as Object.is? No. Object.is(0, -0) is false, while SameValueZero treats them as equal. Set and Map keys use SameValueZero too.
  • Implement lastIndexOf. Walk from the end down to 0, still with === and still skipping holes. Its fromIndex defaults to length - 1 and is the index to start searching backwards from.
  • How would you find an object by its contents? includes and indexOf cannot. Use some or findIndex with 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 Set once and use has, which is O(1) on average.

Common mistakes

  • Using === in includes, so NaN is never found.
  • Using Object.is in includes, which wrongly treats 0 and -0 as different.
  • Skipping holes in includes, or not skipping them in indexOf.
  • 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, so if (arr.myIncludes(x)) would fail for the first element.