Skip to main content
⚑ OpenDevDocs
JavaScript
programmingjavascriptGuideBeginner

JavaScript Arrays and Array Methods

Mastering modern JavaScript arrays: map, filter, reduce, find, some, every, flatMap, slice vs splice, and immutable transformations.

Verified: 2026-09-30
Tested on:node 22.x
Platforms:
webnode
#javascript#arrays#map#filter#reduce#immutability

JavaScript Arrays and Array Methods

Arrays are ordered, zero-indexed collections of values capable of storing mixed data types and executing functional transformations.


1. Immutable Iteration Methods (Preferred)

Modern functional JavaScript avoids manual for-loops in favor of declarative, immutable array methods:

const numbers = [1, 2, 3, 4, 5];

// 1. map(): Transform every item -> returns new array of same length
const doubled = numbers.map(n => n * 2); // [2, 4, 6, 8, 10]

// 2. filter(): Keep items matching predicate -> returns new array
const evens = numbers.filter(n => n % 2 === 0); // [2, 4]

// 3. reduce(): Accumulate array into a single value (sum, object, map)
const sum = numbers.reduce((accumulator, current) => accumulator + current, 0); // 15

2. Searching and Testing Methods

const users = [
  { id: 1, name: "Alex", active: true },
  { id: 2, name: "Sam", active: false },
  { id: 3, name: "Taylor", active: true },
];

// find(): Returns the FIRST matching element (or undefined)
const user = users.find(u => u.id === 2); // { id: 2, name: "Sam", ... }

// some(): Returns true if AT LEAST ONE element matches
const hasInactive = users.some(u => !u.active); // true

// every(): Returns true if ALL elements match
const allActive = users.every(u => u.active); // false

// includes(): Primitive value check
const tags = ["react", "nextjs", "node"];
tags.includes("nextjs"); // true

3. slice() vs. splice()

MethodMutates Original Array?ReturnsTypical Usage
slice(start, end)❌ No (Immutable)Shallow copy of selected portionPagination, copying sub-arrays
splice(start, deleteCount, ...items)βœ… Yes (Mutating)Removed items arrayIn-place removal or insertion
const letters = ["a", "b", "c", "d"];

// slice() does not modify letters:
const sub = letters.slice(1, 3); // ["b", "c"]
console.log(letters);            // ["a", "b", "c", "d"]

// splice() modifies letters in-place:
letters.splice(1, 2);            // Removes "b" and "c"
console.log(letters);            // ["a", "d"]

On this page