JavaScript map() function

CodingTute

Updated on:

JavaScript

In this article, you will learn about the Javascript map() function. The map() function is an array function that iterates through a given array and produces a new array base on the function passed to it.

The map() method iterates the array, for every element of the array the map method calls the function passed to it and produces the values for the new array based on the results it returned.

map() Method Example

const arr = [1, 2, 3, 4];
// pass a function to map
const newArr = arr.map(x => x * 2);
console.log(map);
// expected output: Array [2, 4, 6, 8]

Note: map() doesn’t change the values in the array arr. It only generates the new array based on the function passed to it.

Syntax

map(callbackFn)
map((element) => { /* ... */ })
map((element, index) => { /* ... */ })
map((element, index, array) => { /* ... */ })

Explanation

map() calls the provided function once for each element in the ascending order of index.

  • callbackFn – call back function.
  • element – array element
  • index – array current index
  • array – array object which is processing.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

Related Queries:

  • javascript map
  • map javascript
  • map in javascript
  • .map javascript
  • javascript map function