Javascript map() Array Method

How to use the javascript map() array method.

How to use the javascript map array method.png

Below shows a typical use of the array method map. Note use of the arrow function. If the arrow function contains only one statement, the curly brackets and return keyword can be omitted as shown below. Unlike the foreach array method, map returns a new array so is considered a pure function since it does not alter the input array (users in this case).

const users = ['john','derek','tracey','anna'];
const maptorocks = users.map(user => user + " totally rocks!");
console.log(maptorocks);

Output will be:

[ 'john totally rocks!',
  'derek totally rocks!',
  'tracey totally rocks!',
  'anna totally rocks!' ]

Author