πŸš€ DevOps Certified Professional
πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 | 🀝 +1 (469) 756-6329 πŸ” Contact@DevOpsSchool.com

Array map() Method In JavaScript

DevOps

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally map() method is used to iterate over an array and calling function on every element of array.


Syntax:

array.map(function(currentValue, index, arr), thisValue)

Parameters: This method accepts two parameters as mentioned above and described below:

  • function(currentValue, index, arr): It is required parameter and it runs on each element of array. It contains three parameters which are listed below:
    • currentValue: It is required parameter and it holds the value of current element.
    • index: It is optional parameter and it holds the index of current element.
    • arr: It is optional parameter and it holds the array.
  • thisValue: It is optional parameter and used to hold the value of passed to the function.

Return Value: It returns a new array and elements of arrays are result of callback function.

Below examples illustrate the use of array map() method in JavaScript:
Example 1: This example use array map() method and return the square of array element.

Output:

Example 2: