Skip to main content

Command Palette

Search for a command to run...

Array Methods in JavaScript

Updated
6 min read

JavaScript provides powerful abstractions that help developers write cleaner, declarative, and more readable code. One such core concept is Array Methods.

JavaScript Array Methods.

Most modern array methods are higher order functions because they accept a callback.

Below is a detailed breakdown of commonly used array methods.

forEach()

Purpose

Iterate over each element in an array.

Syntax

array.forEach((element, index, array) => {});

Parameters

  • element (current value)

  • index (optional)

  • array (optional)

Return Value

  • undefined

Mutation

  • Does NOT mutate the original array
    (But you can mutate external variables)

Example

const nums = [1, 2, 3];

nums.forEach(num => {
  console.log(num * 2);
});

filter()

Purpose

Select elements based on a condition.

Syntax

array.filter((element, index, array) => boolean);

Parameters

  • element

  • index (optional)

  • array (optional)

Return Value

  • New array (length ≤ original)

Mutation

  • Does NOT mutate the original array

Example

const nums = [1, 2, 3, 4];

const even = nums.filter(num => num % 2 === 0);

map()

Purpose

Transform each element and return a new array.

Syntax

array.map((element, index, array) => {});

Parameters

  • element

  • index (optional)

  • array (optional)

Return Value

  • New array (same length)

Mutation

  • Does NOT mutate the original array

Example

const nums = [1, 2, 3];

const doubled = nums.map(num => num * 2);

reduce()

Purpose

Reduce an array to a single value.

Syntax

array.reduce((accumulator, current, index, array) => {}, initialValue);

Parameters

  • accumulator

  • current

  • index (optional)

  • array (optional)

  • initialValue (optional but recommended)

Return Value

  • Single value (number, object, array, etc.)

Mutation

  • Does NOT mutate the original array

Example

const nums = [1, 2, 3];

const sum = nums.reduce((acc, curr) => acc + curr, 0);

sort()

Purpose
Sort elements.

Syntax

array.sort((a, b) => number);

Parameters

  • a → First element for comparison

  • b → Second element for comparison

Return Value

  • Sorted array

Mutation

  • Mutates the original array (Mutable)

Example

const nums = [3, 1, 2];
nums.sort((a, b) => a - b);

find()

Purpose

Find the first element that satisfies a condition.

Syntax

array.find((element, index, array) => boolean);

Parameters

  • element → Current element

  • index → Index of current element (optional)

  • array → The original array (optional)

Return Value

  • First matching element

  • undefined if not found

Mutation

  • Does NOT mutate the original array

Example

const users = [{id:1}, {id:2}];

const user = users.find(u => u.id === 2);

some()

Purpose

Check if at least one element satisfies a condition.

Syntax

array.some((element, index, array) => boolean);

Parameters

  • element → Current element

  • index → Index of current element (optional)

  • array → The original array (optional)

Return Value

  • true or false

Mutation

  • Does NOT mutate the original array

Example

const nums = [1, 3, 5];

nums.some(num => num % 2 === 0);

every()

Purpose

Check if all elements satisfy a condition.

Syntax

array.every((element, index, array) => boolean);

Parameters

  • element → Current element

  • index → Index of current element (optional)

  • array → The original array (optional)

Return Value

  • true or false

Mutation

  • Does NOT mutate the original array

Example

const nums = [2, 4, 6];

nums.every(num => num % 2 === 0);

push()

Purpose

Add one or more elements to the end of an array.

Syntax

array.push(element1, element2, ...);

Parameters

  • element(s) to add

Return Value

  • New length of the array

Mutation

  • Mutates the original array (Mutable)

Example

const arr = [1, 2];
arr.push(3);

pop()

Purpose

Remove the last element from an array.

Syntax

array.pop();

Parameters

  • None

Return Value

  • The removed element

Mutation

  • Mutates the original array (Mutable)

Example

const arr = [1, 2, 3];
arr.pop();

concat()

Purpose

Merge arrays into a new array.

Syntax

array.concat(value1, value2, ...);

Parameters

  • One or more arrays / values

Return Value

  • New merged array

Mutation

  • Does NOT mutate the original array (Immutable)

Example

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

slice()

Purpose

Extract a portion of an array.

Syntax

array.slice(startIndex, endIndex);

Parameters

  • start index

  • end index (optional, exclusive)

Return Value

  • New array

Mutation

  • Does NOT mutate the original array (Immutable)

Example

const arr = [1, 2, 3, 4];
const sliced = arr.slice(1, 3);

splice()

Purpose

Add / remove / replace elements at a specific index.

Syntax

array.splice(startIndex, deleteCount, item1, item2, ...);

Parameters

  • start index

  • delete count

  • items to insert (optional)

Return Value

  • Array of removed elements

Mutation

  • Mutates the original array (Mutable)

Example

const arr = [1, 2, 3];
arr.splice(1, 1, 99);

join()

Purpose

Convert array elements into a string.

Syntax

array.join(separator);

Parameters

  • separator (string)

Return Value

  • String

Mutation

  • Does NOT mutate the original array (Immutable)

Example

const arr = ["JS", "is", "fun"];
const result = arr.join(" ");

flat()

Purpose

Flatten nested arrays.

Syntax

array.flat(depth);

Parameters

  • depth (optional, default = 1)

Return Value

  • New flattened array

Mutation

  • Does NOT mutate the original array (Immutable)

Example

const arr = [1, [2, [3]]];
const flatArr = arr.flat(2);

findIndex()

Purpose

Find the index of the first matching element.

Syntax

array.findIndex((element, index, array) => boolean);

Parameters

  • element → Current element being processed

  • index → Index of current element (optional)

  • array → The original array (optional)

Return Value

  • Index

  • -1 if not found

Mutation

  • Immutable

Example

const nums = [5, 10, 15];
const index = nums.findIndex(n => n > 8);

indexOf()

Purpose

Find index of a value (strict comparison).

Syntax

array.indexOf(searchElement, fromIndex);

Parameters

  • searchElement → The value to search for.

  • fromIndex → Index to start searching from (optional)

Return Value

  • Index or -1

Mutation

  • Immutable

Example

const arr = [10, 20, 30];
arr.indexOf(20);

includes()

Purpose

Check if a value exists in the array.

Syntax

array.includes(searchElement, fromIndex);

Parameters

  • searchElement → The value to search for.

  • fromIndex → Index to start searching from(optional)

Return Value

  • true or false

Mutation

  • Immutable

Example

const arr = [1, 2, 3];
arr.includes(2);

Mutating vs Non-Mutating Array Methods

Mutating Methods

These change the original array:

  • push

  • pop

  • shift

  • unshift

  • splice

  • sort

  • reverse

Non-Mutating Methods

These return a new array/value:

  • map

  • filter

  • reduce

  • find

  • some

  • every

  • slice