On this page
Tracks

Polyfill: find, findIndex, findLast, findLastIndex, some and every

Last reviewed 5 Oct 2026

Problem

Implement myFind, myFindIndex, myFindLast, myFindLastIndex, mySome and myEvery on Array.prototype, matching the built-ins:

[5, 12, 8, 130].myFind((x) => x > 10); // 12
[5, 12, 8, 130].myFindIndex((x) => x > 10); // 1
[5, 12, 8, 130].myFindLast((x) => x > 10); // 130
[5, 12, 8, 130].myFindLastIndex((x) => x > 10); // 3
[5, 12, 8, 130].mySome((x) => x > 100); // true
[5, 12, 8, 130].myEvery((x) => x > 100); // false

Clarifying questions

  • Does the callback receive (element, index, array) and is there a thisArg? Yes to both.
  • What is returned when nothing matches? undefined from find and findLast, -1 from findIndex and findLastIndex.
  • Should the loop stop at the first match? Yes. All six stop as soon as the answer is known.
  • What do some and every return for an empty array? some returns false, every returns true.
  • Should holes in sparse arrays be skipped? Only by some and every. The find family visits a hole and passes undefined.

Approach

All six are a loop that returns from inside as soon as the callback gives the deciding answer, with a fallback return after the loop for when it never does:

MethodReturns inside the loop when the callback isReturns after the loop
find / findLasttruthy → the elementundefined
findIndex / findLastIndextruthy → the index-1
sometruthy → truefalse
everyfalsy → falsetrue

findLast and findLastIndex are the same loops walking from the last index down to 0.

The one trap is holes. some and every skip them with i in this, like map and filter. The find family does not: it calls the callback for every index from 0 to length - 1.

Step-by-step build

Step 1 — myFindIndex and myFind

Array.prototype.myFindIndex = function (cb, thisArg) {
for (let i = 0; i < this.length; i++) {
if (cb.call(thisArg, this[i], i, this)) return i;
}
return -1;
};
Array.prototype.myFind = function (cb, thisArg) {
for (let i = 0; i < this.length; i++) {
const value = this[i];
if (cb.call(thisArg, value, i, this)) return value;
}
return undefined;
};

There is no i in this check. myFind saves the element in value before calling the callback, so it returns the element the callback was shown even if the callback changes the array.

Step 2 — myFindLastIndex and myFindLast

Array.prototype.myFindLastIndex = function (cb, thisArg) {
for (let i = this.length - 1; i >= 0; i--) {
if (cb.call(thisArg, this[i], i, this)) return i;
}
return -1;
};
Array.prototype.myFindLast = function (cb, thisArg) {
for (let i = this.length - 1; i >= 0; i--) {
const value = this[i];
if (cb.call(thisArg, value, i, this)) return value;
}
return undefined;
};

Step 3 — mySome

Array.prototype.mySome = function (cb, thisArg) {
for (let i = 0; i < this.length; i++) {
if (i in this && cb.call(thisArg, this[i], i, this)) return true;
}
return false;
};

Step 4 — myEvery

Array.prototype.myEvery = function (cb, thisArg) {
for (let i = 0; i < this.length; i++) {
if (i in this && !cb.call(thisArg, this[i], i, this)) return false;
}
return true;
};

On an empty array neither loop body runs, so the fallback decides: false for some, true for every.

Step 5 — validate the callback and read the length once

if (typeof cb !== 'function') throw new TypeError(cb + ' is not a function');
const len = this.length;

Final code

Array.prototype.myFind = function (cb, thisArg) {
if (typeof cb !== 'function') throw new TypeError(cb + ' is not a function');
const len = this.length;
for (let i = 0; i < len; i++) {
const value = this[i];
if (cb.call(thisArg, value, i, this)) return value;
}
return undefined;
};
Array.prototype.myFindIndex = function (cb, thisArg) {
if (typeof cb !== 'function') throw new TypeError(cb + ' is not a function');
const len = this.length;
for (let i = 0; i < len; i++) {
if (cb.call(thisArg, this[i], i, this)) return i;
}
return -1;
};
Array.prototype.myFindLast = function (cb, thisArg) {
if (typeof cb !== 'function') throw new TypeError(cb + ' is not a function');
for (let i = this.length - 1; i >= 0; i--) {
const value = this[i];
if (cb.call(thisArg, value, i, this)) return value;
}
return undefined;
};
Array.prototype.myFindLastIndex = function (cb, thisArg) {
if (typeof cb !== 'function') throw new TypeError(cb + ' is not a function');
for (let i = this.length - 1; i >= 0; i--) {
if (cb.call(thisArg, this[i], i, this)) return i;
}
return -1;
};
Array.prototype.mySome = function (cb, thisArg) {
if (typeof cb !== 'function') throw new TypeError(cb + ' is not a function');
const len = this.length;
for (let i = 0; i < len; i++) {
if (i in this && cb.call(thisArg, this[i], i, this)) return true;
}
return false;
};
Array.prototype.myEvery = function (cb, thisArg) {
if (typeof cb !== 'function') throw new TypeError(cb + ' is not a function');
const len = this.length;
for (let i = 0; i < len; i++) {
if (i in this && !cb.call(thisArg, this[i], i, this)) return false;
}
return true;
};

Edge cases

  • Empty array: find and findLast return undefined, findIndex and findLastIndex return -1, some returns false, every returns true. The callback is never called.
  • Holes: [1, , 3].myFind((x) => x === undefined) returns undefined because it matched the hole at index 1 — myFindIndex with the same callback returns 1. mySome with that callback returns false, because it never visits the hole.
  • A matching element that is itself undefined looks the same as “not found” from find. Use findIndex when you need to tell them apart.
  • The callback returns a truthy or falsy value that is not a boolean (1, '', an object): it is treated by truthiness, and some / every still return a real true or false.
  • The callback pushes to the array while looping: elements added later are not visited, because len was read before the loop.

Follow-ups

  • Why is [].every(fn) true? “Every element passes” is only false when some element fails, and an empty array has none. It also keeps a.every(fn) && b.every(fn) equal to [...a, ...b].every(fn).

  • Write every using some. Every element passes when no element fails:

    const every = (arr, fn) => !arr.mySome((x, i, a) => !fn(x, i, a));
  • How is find different from filter? find returns the first matching element and stops; filter visits everything and returns an array of all matches.

  • How is findIndex different from indexOf? findIndex takes a callback, so it can match on any condition. indexOf takes a value and compares with ===. See includes and indexOf.

  • How would you stop a forEach early? You cannot — reach for some (return true to stop) or every (return false to stop) instead.

Common mistakes

  • Skipping holes in find and findIndex with i in this. Only some and every skip them.
  • Returning -1 from find, or undefined from findIndex, when nothing matches.
  • Returning false from every on an empty array.
  • Returning the callback’s raw result from some or every instead of true / false.
  • Implementing findLast as [...this].reverse().find(cb): it copies the array and passes the wrong index to the callback.
  • In the backward loops, stopping at i > 0 and missing index 0.