PHP array_map() Equivalent in JavaScript

JavaScript's Array.prototype.map() is a near-exact match for PHP's array_map() on plain lists, but PHP's key-preserving and multi-array behavior have no direct one-line equivalent.

Close equivalent Covers the common case, but has documented behavioral differences.

PHP source
array_map()
Closest JavaScript API
Array.prototype.map()

Main limitation: JavaScript `Array.prototype.map()` is eager (returns a new array immediately), matching PHP's behavior — unlike Python's lazy `map()`.

How to use each function

Before comparing the two, here's how array_map() and Array.prototype.map() work on their own.

PHP: array_map()

array_map(callable $callback, array $array, array ...$arrays): array

Applies a callback to every element of one or more arrays and returns a new array of the results. When multiple arrays are passed, the callback receives one element from each array per call, and PHP pads any shorter array with null.

Returns: A new array with the same length as the longest input array.

PHP
$prices = [10, 20, 30];
$withTax = array_map(fn($p) => $p * 1.2, $prices);
// [12, 24, 36]

JavaScript: Array.prototype.map()

arr.map(callbackFn(element, index, array), thisArg)

Calls callbackFn once for every element and returns a new array of the results, immediately and in order — eager, like PHP's array_map(), unlike Python's lazy map(). It only iterates a single array natively; combining a second array means reading it by index inside the callback.

Returns: A new array of the same length as the original.

JavaScript
[10, 20, 30].map((n) => n * 1.2);
// [12, 24, 36]

Quick mapping

PHP array_map()
Closest JavaScript API Array.prototype.map()

How it differs

  • JavaScript Array.prototype.map() is eager (returns a new array immediately), matching PHP's behavior — unlike Python's lazy map().
  • PHP's array_map() preserves string keys on a single associative array; JavaScript arrays don't have string keys in the same sense, so converting an object's values requires Object.values(obj).map(...) or Object.fromEntries() if you need to keep the keys.
  • PHP array_map() accepts multiple arrays and passes corresponding elements to the callback as separate arguments; JavaScript's map() only iterates a single array — combine arrays first with something like index-based access, or restructure the data.

Examples

Doubling every element

PHP
$doubled = array_map(fn($n) => $n * 2, [1, 2, 3]);
// [2, 4, 6]
JavaScript
const doubled = [1, 2, 3].map((n) => n * 2);
// [2, 4, 6]
Both are eager and return a new array of the same length — a direct, exact-feeling translation for plain lists.

Multiple source arrays

PHP
array_map(fn($a, $b) => $a + $b, [1, 2], [10, 20]);
// [11, 22]
JavaScript
[1, 2].map((a, i) => a + [10, 20][i]);
// [11, 22]
JavaScript's map() only receives one array natively, so combining a second array requires using the callback's index parameter to look up the corresponding element.

Edge cases

Edge case

JavaScript Array.prototype.map() always returns an array of the same length as the input, including for sparse arrays (it visits holes but the callback still runs for indices that exist) — verify behavior with sparse arrays if your PHP code assumes reindexing.

References

  • PHP Manual — array_map — checked 2026-07-29
  • MDN Web Docs — Array.prototype.map() — checked 2026-07-29

Last verified 2026-07-29.