How to use the map() method in JavaScript ?

The map() method in JavaScript is used to create a new array with the results of calling a provided function on every element in the calling array.

Here’s an example of how to use the map() method:

let numbers = [1, 2, 3, 4, 5];
let doubledNumbers = numbers.map(function(number) {
  return number * 2;
});
console.log(doubledNumbers); // [2, 4, 6, 8, 10]

In this example, the map() method is called on the numbers array, and the function passed to map() doubles each element in the array. The returned value of the map() method is a new array containing the doubled numbers.

You can also use arrow function as well:

let numbers = [1, 2, 3, 4, 5];
let doubledNumbers = numbers.map(number => number * 2);
console.log(doubledNumbers); // [2, 4, 6, 8, 10]

As you can see, map returns a new Array, it does not change the original array.