DEV Community

vishwa v
vishwa v

Posted on • Edited on

Methods-2

JavaScript Array forEach()

The forEach() method calls a function (a callback function) for each array element.

const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);

function myFunction(value, index, array) {
  txt += value + "<br>";
}
Enter fullscreen mode Exit fullscreen mode

JavaScript Array map()
The map() method creates a new array by performing a function on each array element.

The map() method does not execute the function for array elements without values.

const numbers1 = [45, 4, 9, 16, 25];
const numbers2 = numbers1.map(myFunction);

function myFunction(value, index, array) {
  return value * 2;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)