JS Built-ins
← Array132 / 226
ArrayArray.prototype.mapbeginner

Array.prototype.map() β€” double each number

πŸ“– Usage Example
const nums = [1, 2, 3]
nums.map(n => n * 2)  // β†’ [2, 4, 6]

Use map() to transform every element of an array and collect the results in a new array.

Array.prototype.map()

Array.prototype.map(callback) creates a new array populated with the return values of calling callback on every element of the original array. The original array is not mutated.

Challenge: Implement double(nums) that returns a new array where every number is multiplied by 2.

double([1, 2, 3])    // β†’ [2, 4, 6]
double([0, -1, 10])  // β†’ [0, -2, 20]
πŸ’‘ Hints (2)
  • β€’ `map` always returns a new array of the same length.
  • β€’ The callback receives the element as the first argument: `x => x * 2`.