A price list in dollars must be shown in euros, with labels. How do you turn every element of an array into something new without changing the original?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
map returns a new array of the same length, built from each callback's return value.
Learning objective: Use map to transform every element into a new array, return values correctly from callbacks, map arrays of objects to new shapes, and keep the original unchanged.
Skills practised: Functional array processing, Transforming with map.
04Explanation
list.map(fn) calls fn for each element and collects the return values into a new array of the same length.
The original array is not changed. You get a new array: const labels = prices.map((p) => "$" + p);.
The callback must return a value. With braces { } you need return; without braces the expression is returned automatically.
map is perfect for turning objects into strings for display, or into new objects with a different shape: users.map((u) => u.name).
map is a pure transformation when its callback is pure; avoid side effects inside map. If you only need side effects, use forEach.
Mapping objects to new objects with spread ({ ...item, total: item.qty * item.price }) keeps the original objects untouched; mutating item.total = ... inside map changes the source data.
map passes (element, index, array). ["1", "2", "3"].map(parseInt) gives [1, NaN, NaN] because parseInt receives the index as its radix. Wrap such functions: .map((s) => parseInt(s, 10)) or use .map(Number).
Array.from(iterable, mapFn) maps while converting, for example Array.from({ length: 5 }, (_, i) => i * i).
05Definitions
map
Creates a new array from the return value of a function called on each element.
Transformation
Turning each input value into an output value.
Pure function
A function whose result depends only on its inputs and that changes nothing outside.
Projection
Mapping objects to just the properties you need, such as names.
Implicit return
An arrow function without braces returns its expression.
An account page receives order objects from the server and maps them into display rows: date, item count and formatted total. The server data is never modified.
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Return from the callback: Prints the names; capitalised is correct
Convert currency: Prints euros; usd unchanged; Uses map
Student report cards: Prints the cards; toCard works alone; students unchanged
ex-032-01: output matches the documented expected output
ex-032-02: output matches the documented expected output
ex-032-03: output matches the documented expected output
ex-032-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-07-functional-array-processing/lesson-032-map-and-transformations/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
map returns a new array of the same length.
The callback must return the new value.
map does not change the original, unless the callback mutates objects.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationmap transformations
Context: I am learning map transformations in JavaScript.
Task: Explain map with prices and objects, including why the callback must return a value.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionmap transformations drill
Context: I have practised map transformations.
Task: Give me six map calls and ask for the resulting arrays, including one parseInt trap.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingmap transformations bug
Context: My map returns an array full of undefined.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationmap transformations practice
Context: I finished the map transformations exercises.
Task: Create three map exercises that reshape arrays of objects.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewmap transformations review
Context: Here is my code that uses map transformations.
Task: Review my map callbacks for missing returns and accidental mutation.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.