Member-only story

Javascript Array Map — A Complete guide

Vinodh Thangavel
5 min readJul 6, 2023

--

Map is one of the most used Array method, from simple transformation logics to displaying list of items in React, we deep dive into all it can do

map

Syntax — array.map(function(iterateValue, index, array), thisValue)

Arguments —

  • callback function (R)— this will be called for every items in the array
  • thisValue (O) — Whatever value we pass here can be assesed with this inside the callback function

Callback function Arguments —

  • iterateValue (O)— Value of the current element of the array
  • index (O)— Index of the current element
  • array (O) — Source array to the map method

R — Required , O — Optional Parameter

const marks = [98, 100, 50, 75, 90];
const callbackFn = function () {
return 'Some Value';
}
const transformedMarks = marks.map(callbackFn);
console.log(transformedMarks);
// ["Some Value","Some Value","Some Value","Some Value","Some Value"]

Code Analysis

  • marks.map(callbackFn);marks is the source array and we are passing the callbackFn as argument.
  • marks array will be iterated and callback function will be called with each iteration
  • Notice, we are having an empty callback and we are not using the value from the marks .
  • Whatever we return from the callback

--

--

Vinodh Thangavel
Vinodh Thangavel

Written by Vinodh Thangavel

Passionate lifelong learner, coding enthusiast, and dedicated mentor. My journey in tech is driven by curiosity, creativity, and a love for sharing knowledge.

No responses yet