The map method iterates through each element of an array and returns a new array with the results of calling a provided callback function on each element. The callback function is used to transform each element and return a new element, which gets placed in the same index of the new array. Map allows transforming each element of an array easily without using for loops. Other ways to transform arrays include forEach, for..of loops, and regular for loops but map provides a cleaner syntax for one-to-one transformations of each element.
8. Other ways than map
for..of
const array = [1, 2, 3]
const new_array = []
for ( let x of array)
new_array.push(x * x )
console.log(new_array) // [1, 4, 9]
9. Other ways than map
for
const array = [1, 2, 3]
const new_array = []
for ( let x = 0; x < array.length; x++)
new_array.push(array[x] * array[x])
console.log(new_array) // [1, 4, 9]
10. And finally Map
const array = [1, 2, 3]
const new_array = array.map( x => x * x)
console.log(new_array) // [1, 4, 9]