map function demo 1

map function demo

const array = [2, 5, 9];
let squares = array.map((num) => num * num);

console.log(array); // [2, 5, 9]
console.log(squares); // [4, 25, 81]

Here is what the above code is Doing:
1. We have an array of numbers.
2. We use the map method to iterate through each element in the array.
3. For each element, we return the element multiplied by itself.
4. The map method returns a new array with each element being the result of the callback function.

Similar Posts