JavaScript Interview preparation

๐Ÿ”น Array Polyfills

  1. Array.prototype.map
  2. Array.prototype.filter
  3. Array.prototype.reduce
  4. Array.prototype.forEach
  5. Array.prototype.find
  6. Array.prototype.findIndex
  7. Array.prototype.some
  8. Array.prototype.every
  9. Array.prototype.flat
  10. Array.prototype.includes
  11. Array.prototype.indexOf
  12. Array.prototype.push / pop / shift / unshift (sometimes asked for basics)

๐Ÿ”น String Polyfills

  1. String.prototype.trim
  2. String.prototype.includes
  3. String.prototype.startsWith
  4. String.prototype.endsWith
  5. String.prototype.repeat

๐Ÿ”น Object Polyfills

  1. Object.assign
  2. Object.create
  3. Object.keys
  4. Object.values
  5. Object.entries
  6. Object.fromEntries

๐Ÿ”น Function Polyfills

  1. Function.prototype.call
  2. Function.prototype.apply
  3. Function.prototype.bind
  4. debounce (custom utility, not native but very common in interviews)
  5. throttle (same as above)
  6. once (utility to allow a function to run only once)

๐Ÿ”น Promise & Async Polyfills

  1. Promise.all
  2. Promise.race
  3. Promise.allSettled
  4. Promise.any
  5. 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

  • forEach loops 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 for 1 and 3).

๐Ÿ”น 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.
  • this refers to the array on which myForEach is 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);

  • call is used to set the this context inside the callback (if provided as thisArg).
  • 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)

  1. Returns undefined โ†’ not chainable.
  2. No early exit โ†’ unlike for or for..of, you cannot break or return.
  3. Handles sparse arrays correctly.
  4. Time Complexity: O(n) โ†’ runs once for each element.
  5. Space Complexity: O(1) โ†’ no extra memory (just runs callbacks).