forEach() and map()?

  • forEach() → Executes a function on each item, but returns undefined.
  • map() → Transforms elements and returns a new array.

Example:

let arr = [1, 2, 3];
arr.forEach(x => console.log(x * 2)); // prints 2, 4, 6
console.log(arr.map(x => x * 2)); // [2, 4, 6]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *