Javascript Array Methods

Comprehensive reference for javascript array methods covering essential concepts, practical examples, and production best practices.

Javascript Array Methods are the foundational tools for manipulating, transforming, and querying arrays in JavaScript. Use them when you need to iterate, filter, transform, or reduce data in a predictable, functional way—whether processing API responses, building state updates in React, or preparing data for a charting library. These methods are not just convenient; they are the primary way to express intent in modern JavaScript.

Transforming Arrays

Map: Transform each element into a new value

The map method creates a new array by applying a function to each element of the original array.

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

const users = [
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 25 }
];

const userNames = users.map(u => u.name);
// ['Alice', 'Bob']

// Map with index and array context
const indexed = users.map((u, i, arr) => ({
  id: i,
  fullName: u.name,
  isLast: i === arr.length - 1
}));

Key edge cases:

Common failure:

Filter: Select elements based on a condition

filter returns a new array containing only the elements that pass a test function.

const scores = [85, 92, 78, 96, 70];
const passing = scores.filter(s => s >= 80);
// [85, 92, 96]

const adults = users.filter(u => u.age >= 18);
// [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]

// Chaining with map
const highScores = scores
  .filter(s => s > 80)
  .map(s => s * 1.1) // bonus points
  .filter(s => s > 100); // final cutoff

Key edge cases:

Common failure:

Reduce: Accumulate values into a single result

reduce applies a function to accumulate a value across the array.

const prices = [10, 15, 20, 5];

// Sum all prices
const total = prices.reduce((sum, price) => sum + price, 0);
// 50

// Group users by age
const grouped = users.reduce((acc, user) => {
  const key = user.age;
  if (!acc[key]) acc[key] = [];
  acc[key].push(user);
  return acc;
}, {});

// Result: { 30: [{ name: 'Alice', age: 30 }], 25: [{ name: 'Bob', age: 25 }] }

// Flatten nested arrays
const nested = [[1, 2], [3, 4], [5]];
const flat = nested.reduce((acc, arr) => acc.concat(arr), []);
// [1, 2, 3, 4, 5]

Key edge cases:

> TypeError: Reduce of empty array with no initial value

Common failure:

Querying Arrays

Find: Get the first matching element

find returns the first element that satisfies a condition.

const users = [
  { name: 'Alice', role: 'admin' },
  { name: 'Bob', role: 'user' },
  { name: 'Charlie', role: 'admin' }
];

const admin = users.find(u => u.role === 'admin');
// { name: 'Alice', role: 'admin' }

const user = users.find(u => u.name === 'Charlie');
// { name: 'Charlie', role: 'admin' }

// Find with index
const firstUser = users.find((u, i) => u.role === 'user' && i === 0);
// { name: 'Bob', role: 'user' }

Key edge cases:

Common failure:

FindIndex: Get the index of the first matching element

findIndex returns the index of the first matching element.

const numbers = [10, 20, 30, 40];

const index = numbers.findIndex(n => n > 25);
// 2

const userIndex = users.findIndex(u => u.name === 'Bob');
// 1

// Find index of first even number
const evenIndex = numbers.findIndex(n => n % 2 === 0);
// 0

Key edge cases:

Common failure:

Includes: Check for presence of a value

includes checks whether a value exists in the array.

const fruits = ['apple', 'banana', 'cherry'];

fruits.includes('banana');
// true

fruits.includes('grape');
// false

// With fromIndex
fruits.includes('apple', 1);
// false — starts search from index 1, so 'apple' not found

// With object references
const fruitsList = [fruits, ['kiwi', 'mango']];
fruitsList.includes(fruits);
// true

const fruitCopy = [...fruits];
fruitsList.includes(fruitCopy);
// false — different reference, even though values match

Key edge cases:

Common failure:

Mutating Arrays

Push: Add elements to the end

push adds one or more elements to the end of an array and returns the new length.

const tasks = ['learn JS', 'build API'];

const newLength = tasks.push('deploy', 'test');
// tasks = ['learn JS', 'build API', 'deploy', 'test']
// newLength = 4

Key edge cases:

Common failure:

Unshift: Add elements to the beginning

unshift adds one or more elements to the beginning of an array and returns the new length.

const logEvents = ['error', 'warning'];

const newLength = logEvents.unshift('info', 'debug');
// logEvents = ['info', 'debug', 'error', 'warning']
// newLength = 4

Key edge cases:

Common failure:

Pop: Remove last element

pop removes the last element from the array and returns it.

const stack = [1, 2, 3];
const last = stack.pop();
// last = 3, stack = [1, 2]

Key edge cases:

Common failure:

Shift: Remove first element

shift removes the first element and returns it.

const queue = ['first', 'second', 'third'];
const first = queue.shift();
// first = 'first', queue = ['second', 'third']

Key edge cases:

Common failure:

Combining and Splitting Arrays

Concat: Combine arrays

concat creates a new array by merging multiple arrays or values.

const a = [1, 2];
const b = [3, 4];

const combined = a.concat(b);
// [1, 2, 3, 4]

const withValues = a.concat(5, 6);
// [1, 2, 5, 6]

const nested = a.concat([b, [7, 8]]);
// [1, 2, [3, 4], [7, 8]]

Key edge cases:

Common failure:

Slice: Extract a portion of an array

slice returns a shallow copy of a portion of the array.

const data = [10, 20, 30, 40, 50];

const subset = data.slice(1, 3);
// [20, 30]

const firstThree = data.slice(0, 3);
// [10, 20, 30]

const lastTwo = data.slice(-2);
// [40, 50]

// Clone array
const copy = data.slice();
// [10, 20, 30, 40, 50]

Key edge cases:

Common failure:

Splice: Insert, remove, and replace elements

splice modifies an array in place and returns the removed elements.

const colors = ['red', 'green', 'blue'];

// Remove 1 element from index 1
const removed = colors.splice(1, 1);
// colors = ['red', 'blue'], removed = ['green']

// Add elements at index 1
colors.splice(1, 0, 'yellow', 'purple');
// colors = ['red', 'yellow', 'purple', 'blue']

// Replace 2 elements starting at index 0
colors.splice(0, 2, 'orange', 'pink');
// colors = ['orange', 'pink', 'purple', 'blue'], removed = ['red', 'yellow']

Key edge cases:

Common failure:

Utility Methods

Reverse: Invert array order

reverse reverses the array in place and returns the same array.

const items = ['a', 'b', 'c'];
const reversed = items.reverse();
// items = ['c', 'b', 'a'], reversed = ['c', 'b', 'a']

Key edge cases:

Common failure:

Sort: Sort elements

sort sorts elements in place and returns the sorted array.

const nums = [10, 1, 25, 3];
nums.sort();
// [1, 10, 25, 3] — lexicographic (string) sort

const names = ['Alice', 'Bob', 'Charlie'];
names.sort();
// ['Alice', 'Bob', 'Charlie'] — alphabetical

// Custom sort
const numbers = [4, 1, 3, 2];
numbers.sort((a, b) => a - b);
// [1, 2, 3, 4]

Key edge cases:

Common failure:

This page was rewritten on 10 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.