INSTACK 360

JavaScript Arrays & Array Methods

Master data collections in JS. Learn essential higher-order array methods like map, filter, and reduce.

Intermediate 5 min read

Working with Arrays

An array is a special variable, which can hold more than one value at a time. JavaScript arrays are dynamic and can hold mixed data types.

const fruits = ["Apple", "Banana", "Orange"];

Higher-Order Array Methods

Modern JavaScript relies heavily on functional programming concepts using arrays.

1. map()

Creates a new array populated with the results of calling a provided function on every element.

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

2. filter()

Creates a new array with all elements that pass the test implemented by the provided function.

const ages = [32, 15, 20, 12];
const adults = ages.filter(age => age >= 18);
// adults is [32, 20]

3. reduce()

Executes a reducer function on each element, resulting in a single output value.

const sum = numbers.reduce((total, num) => total + num, 0);
// sum is 6