๐น Array Polyfills
- Array.prototype.map
- Array.prototype.filter
- Array.prototype.reduce
- Array.prototype.forEach
- Array.prototype.find
- Array.prototype.findIndex
- Array.prototype.some
- Array.prototype.every
- Array.prototype.flat
- Array.prototype.includes
- Array.prototype.indexOf
- Array.prototype.push / pop / shift / unshift (sometimes asked for basics)
๐น String Polyfills
- String.prototype.trim
- String.prototype.includes
- String.prototype.startsWith
- String.prototype.endsWith
- String.prototype.repeat
๐น Object Polyfills
- Object.assign
- Object.create
- Object.keys
- Object.values
- Object.entries
- Object.fromEntries
๐น Function Polyfills
- Function.prototype.call
- Function.prototype.apply
- Function.prototype.bind
- debounce (custom utility, not native but very common in interviews)
- throttle (same as above)
- once (utility to allow a function to run only once)
๐น Promise & Async Polyfills
- Promise.all
- Promise.race
- Promise.allSettled
- Promise.any
- Custom Promise implementation (basic version)
Array.prototype.myForEach = function(callback, thisArg) {
if (typeof callback !== "function") {
throw new TypeError(callback + " is not a function");
}
for (let i = 0; i < this.length; i++) {
if (i in this) { // skip empty slots in sparse arrays
callback.call(thisArg, this[i], i, this);
}
}
};
// โ
Example
[10, 20, 30].myForEach((val, idx) => {
console.log(`Index: ${idx}, Value: ${val}`);
});
// Output:
// Index: 0, Value: 10
// Index: 1, Value: 20
// Index: 2, Value: 30
Alright ๐ letโs explain the forEach polyfill step by step, so itโs crystal clear for interviews.
๐น Native Behavior Recap
forEachloops over every element of an array.- Calls a callback function for each element.
- Passes
(element, index, array)to the callback. - Does not return anything (always
undefined). - Skips holes in sparse arrays (
[1,,3]โ only runs for1and3).
๐น Polyfill Implementation
Array.prototype.myForEach = function(callback, thisArg) {
// 1. Validate callback
if (typeof callback !== "function") {
throw new TypeError(callback + " is not a function");
}
// 2. Loop over array
for (let i = 0; i < this.length; i++) {
// 3. Skip empty slots in sparse arrays
if (i in this) {
// 4. Call callback with correct `thisArg`
callback.call(thisArg, this[i], i, this);
}
}
};
๐น Breaking Down the Polyfill
1. Validation
if (typeof callback !== "function") {
throw new TypeError(callback + " is not a function");
}
- Ensures that the argument passed is a function.
- Matches native JS behavior (otherwise throws error).
2. Looping
for (let i = 0; i < this.length; i++) {
- Loops through the array.
thisrefers to the array on whichmyForEachis called.
3. Handling Sparse Arrays
if (i in this) {
- Checks if the index actually exists.
- Example:
let arr = [1,,3]; arr.myForEach(x => console.log(x)); // Output: 1, 3 (skips index 1 because itโs empty)
4. Calling the Callback
callback.call(thisArg, this[i], i, this);
callis used to set thethiscontext inside the callback (if provided asthisArg).- Passes element, index, array just like native
forEach.
๐น Example Usage
const arr = [10, 20, 30];
arr.myForEach((value, index, array) => {
console.log(`Value: ${value}, Index: ${index}, Array: ${array}`);
});
// Output:
// Value: 10, Index: 0, Array: 10,20,30
// Value: 20, Index: 1, Array: 10,20,30
// Value: 30, Index: 2, Array: 10,20,30
๐น Key Points (Interview Notes)
- Returns
undefinedโ not chainable. - No early exit โ unlike
fororfor..of, you cannotbreakorreturn. - Handles sparse arrays correctly.
- Time Complexity:
O(n)โ runs once for each element. - Space Complexity:
O(1)โ no extra memory (just runs callbacks).