On this page
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); // falseClarifying questions
- Does the callback receive
(element, index, array)and is there athisArg? Yes to both. - What is returned when nothing matches?
undefinedfromfindandfindLast,-1fromfindIndexandfindLastIndex. - Should the loop stop at the first match? Yes. All six stop as soon as the answer is known.
- What do
someandeveryreturn for an empty array?somereturnsfalse,everyreturnstrue. - Should holes in sparse arrays be skipped? Only by
someandevery. Thefindfamily visits a hole and passesundefined.
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:
| Method | Returns inside the loop when the callback is | Returns after the loop |
|---|---|---|
find / findLast | truthy → the element | undefined |
findIndex / findLastIndex | truthy → the index | -1 |
some | truthy → true | false |
every | falsy → false | true |
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:
findandfindLastreturnundefined,findIndexandfindLastIndexreturn-1,somereturnsfalse,everyreturnstrue. The callback is never called. - Holes:
[1, , 3].myFind((x) => x === undefined)returnsundefinedbecause it matched the hole at index 1 —myFindIndexwith the same callback returns1.mySomewith that callback returnsfalse, because it never visits the hole. - A matching element that is itself
undefinedlooks the same as “not found” fromfind. UsefindIndexwhen 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, andsome/everystill return a realtrueorfalse. - The callback pushes to the array while looping: elements added later are not visited, because
lenwas 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 keepsa.every(fn) && b.every(fn)equal to[...a, ...b].every(fn). -
Write
everyusingsome. Every element passes when no element fails:const every = (arr, fn) => !arr.mySome((x, i, a) => !fn(x, i, a)); -
How is
finddifferent fromfilter?findreturns the first matching element and stops;filtervisits everything and returns an array of all matches. -
How is
findIndexdifferent fromindexOf?findIndextakes a callback, so it can match on any condition.indexOftakes a value and compares with===. See includes and indexOf. -
How would you stop a
forEachearly? You cannot — reach forsome(returntrueto stop) orevery(returnfalseto stop) instead.
Common mistakes
- Skipping holes in
findandfindIndexwithi in this. Onlysomeandeveryskip them. - Returning
-1fromfind, orundefinedfromfindIndex, when nothing matches. - Returning
falsefromeveryon an empty array. - Returning the callback’s raw result from
someoreveryinstead oftrue/false. - Implementing
findLastas[...this].reverse().find(cb): it copies the array and passes the wrong index to the callback. - In the backward loops, stopping at
i > 0and missing index 0.
Related
- Concepts: call, apply and bind in the JavaScript track, for how
cb.call(thisArg, …)setsthis. - Previous: reduce and reduceRight.
- Next: includes and indexOf.