Javascript Array map() for Junior
What is map() function? Map is a collection of elements where each element is stored as a Key, value pair. The map() method is used to apply a function on every element in an array. A new array is then returned.
Array.map function has following signature:
arr.map(function callback(currentValue[, index[, array]])
Example:
const number=[1,2,3,4,5]; let newArr = number.map((val, index, arr) => { // return element to new Array }); newArr — the new array that is returned number — the array to run the map function on val — the current value being processed index — the current index of the value being processed arr — the original array map() Vs for loop Map works same as the for loop the only difference is that map() returns a new array where as for loop change the exiting array or collect elements in a new array.