Skip to content

Instantly share code, notes, and snippets.

@vinicius5581
Last active April 13, 2020 04:01
Show Gist options
  • Save vinicius5581/c1d1876a4f6156a3d05059ca4fd5948b to your computer and use it in GitHub Desktop.
Save vinicius5581/c1d1876a4f6156a3d05059ca4fd5948b to your computer and use it in GitHub Desktop.
Array.prototype
const numbersArray = [1, 2, 3, 4];
const logTimesTwo = num => console.log(num * 2);
const customForEach = (arr, cb) => {
for (let i = 0; i < arr.length; i++) {
cb(arr[i]);
}
}
customForEach(numbersArray, logTimesTwo);
// custom forEach without callback
const numbersArray = [1, 2, 3, 4];
const logTimesTwo = num => console.log(num * 2);
const customForEach = (arr) => {
for (let i = 0; i < arr.length; i++) {
logTimesTwo(arr[i]);
}
}
customForEach(numbersArray);
const numbersArray = [1, 2, 3, 4];
console.log(numbersArray[0]); // 1
console.log(numbersArray[1]); // 2
console.log(numbersArray[2]); // 3
console.log(numbersArray[3]); // 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment