🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Array.map()

AI & DATA SCIENCE // map

map() creates a NEW array by calling a function on every element. It does not mutate the original array.

Syntax

const doubled = [1,2,3].map(n => n * 2);
// [2, 4, 6]

Deep Dive Course

**map()** transforms every element and returns a new array of the same length. It's the functional alternative to a for loop that builds a new array. Always returns an array — if you need to combine or filter, chain with **filter()** or **reduce()**.

1Understanding Array.map()

map() transforms every element and returns a new array of the same length. It's the functional alternative to a for loop that builds a new array. Always returns an array — if you need to combine or filter, chain with filter() or reduce().

💡

map() always returns an array of the same length. If you want to skip elements, use filter(). If you want both, use flatMap() or filter().map().

editor.html
// Transform data
const prices = [10, 20, 30, 40];
const withTax = prices.map(p => p * 1.2);
console.log(withTax); // [12, 24, 36, 48]

// Extract property from array of objects
const users = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }];
const names = users.map(u => u.name);
console.log(names); // ['Alice', 'Bob']
localhost:3000

2Practical Example

Here is a real-world application of Array.map() showing how it is used in production JavaScript code.

editor.html
// map + destructuring
const pairs = [[1, 'one'], [2, 'two'], [3, 'three']];
const obj = Object.fromEntries(pairs.map(([num, word]) => [word, num]));
console.log(obj); // { one: 1, two: 2, three: 3 }
localhost:3000

3Best Practices

Follow these guidelines when working with Array.map():

1. Never use map() just for side effects — use forEach instead

2. Keep map callbacks pure (no external state changes)

3. Chain map() with filter() for data pipelines

⚠️

Tip: map() always returns an array of the same length. If you want to skip elements, use filter(). If you want both, use flatMap() or filter().map().

editor.html
// Transform data
const prices = [10, 20, 30, 40];
const withTax = prices.map(p => p * 1.2);
console.log(withTax); // [12, 24, 36, 48]

// Extract property from array of objects
const users = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }];
const names = users.map(u => u.name);
console.log(names); // ['Alice', 'Bob']
localhost:3000

Examples

Example 01Basic Usage
// Transform data
const prices = [10, 20, 30, 40];
const withTax = prices.map(p => p * 1.2);
console.log(withTax); // [12, 24, 36, 48]

// Extract property from array of objects
const users = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }];
const names = users.map(u => u.name);
console.log(names); // ['Alice', 'Bob']
Example 02Advanced Example
// map + destructuring
const pairs = [[1, 'one'], [2, 'two'], [3, 'three']];
const obj = Object.fromEntries(pairs.map(([num, word]) => [word, num]));
console.log(obj); // { one: 1, two: 2, three: 3 }

Best Practices

  • Never use map() just for side effects — use forEach instead
  • Keep map callbacks pure (no external state changes)
  • Chain map() with filter() for data pipelines

Interview Question

What is the difference between map() and forEach()?

Hint: Return value and purpose.

map() returns a NEW array of transformed values (pure transformation). forEach() always returns undefined and is for side effects only. Use map() when you need a transformed array. Use forEach() when you just want to do something with each element without needing the result.

Exercises

MediumPractice using Array.map() in a real scenario.
View Solution
// Transform data
const prices = [10, 20, 30, 40];
const withTax = prices.map(p => p * 1.2);
console.log(withTax); // [12, 24, 36, 48]

// Extract property from array of objects
const users = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }];
const names = users.map(u => u.name);
console.log(names); // ['Alice', 'Bob']

Frequently Asked Questions

What is the difference between map() and forEach()?

map() returns a NEW array of transformed values (pure transformation). forEach() always returns undefined and is for side effects only. Use map() when you need a transformed array. Use forEach() when you just want to do something with each element without needing the result.

Related Functions

filterreduceforEachflatMap