Skip to content

Instantly share code, notes, and snippets.

@shekhardtu
Created August 3, 2022 18:44
Show Gist options
  • Save shekhardtu/1c2ed226b191e512824f392f0e31d50d to your computer and use it in GitHub Desktop.
Save shekhardtu/1c2ed226b191e512824f392f0e31d50d to your computer and use it in GitHub Desktop.
The forEach polyfils - Javascript Interview Question
Array.prototype.customForEach = function(fn) {
const arr = this;
for(let i =0; i<arr.length; i++) {
fn(arr[i], i, arr)
}
};
const array = [1,2,3,4,5];
array.customForEach((item, i) => {
console.log(item*2)
});
// output: 2 4 6 8 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment