Skip to content

Instantly share code, notes, and snippets.

@SergeyLipko
Created April 19, 2017 10:54
Show Gist options
  • Save SergeyLipko/319da288857a2412db61c3067e5736b2 to your computer and use it in GitHub Desktop.
Save SergeyLipko/319da288857a2412db61c3067e5736b2 to your computer and use it in GitHub Desktop.
How Array.prototype.map() and Array.prototype.forEach() works
const myArr = [1, 2, 3, 4, 5, 6];
// map создает новый массив на основании вызова callback для каждого элемента (трансформация)
const newMap = myArr.map(i => {
return i * 2;
});
// forEach выполняет callback для каждого элемента массива (перебор)
const newForEach = myArr.forEach(i => {
return i * 2;
});
console.log(newMap); // 2, 4, 6, 8, 10, 12
console.log(newForEach); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment