Skip to content

How to use Array map method in Javascript

Published: at 03:53 AM

Array.prototype.map method accepts a callback function as a parameter and iterate over all the items in the array and calls the callback with that value and return a new array with the values returned from the callback function.

Example: Double the values in the array,

const numbers = [1, 2, 3]
const result = numbers.map((number) => number * 2)
console.log(result) // [2, 4, 6]

The callback function is called with three parameters

// Callback function with three parameters syntax
function double(value, index, array) {
  return value * 2
}

// Callback function with two parameters syntax
function double(value, index) {
  return value * 2
}

// Callback function with one parameters syntax
function double(value) {
  return value * 2
}

Here, value is the element in the array. The index is the current element position in the array. And array is the array the map is called with.

When to use it?

When not to use it?

Thanks for reading.