Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Filtering and transforming arrays

filter selects elements that satisfy a condition, while map transforms their values. By default, chaining them creates a new array without directly changing the input array.

Version: ECMAScript: Array.prototype.filter, Array.prototype.map, and arrow-function callback syntax are language features, not Web APIs. Node 24 in the repository does not establish browser support.

Syntax

array.filter((value, index, array) => condition)
array.map((value, index, array) => expression)
array.filter(predicate).map(transform)

filter(predicate) selects elements whose callback result becomes true after Boolean conversion. It preserves their order and values but may reduce the element count. By default, map(transform) stores callback results in a new array whose length matches the input length at the start of the call. It is not an element-removal operation.

For array inputs, map can construct the result using a custom constructor selected via Symbol.species. That constructor can return an object that is not an array. map writes results at the corresponding indexes but does not explicitly set the result object's length at the end.

Both callbacks receive (value, index, array). In array.filter(predicate).map(transform), map operates on the result of filter, so transform receives indexes and the array from that intermediate result. The example first selects even numbers, then multiplies them by 10.

By default, the methods themselves do not mutate the input, but a callback can. A new array is not a deep copy: retained objects are still shared. When an arrow function uses a {} body, remember return; without it, map stores undefined and filter rejects the element.

Both methods skip absent indexes. For an ordinary sparse array, map preserves holes, whereas filter packs the selected elements together. These are ECMAScript language methods, not Web APIs; Node 24 being present in a repository does not establish browser API support.

Examples

const numbers = [1, 2, 3, 4, 5];
const result = numbers
  .filter(value => value % 2 === 0)
  .map(value => value * 10);

console.log(JSON.stringify(result));
console.log(JSON.stringify(numbers));

Result

[20,40]
[1,2,3,4,5]

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources