JavaScript Array Methods

A quick reference for adding, transforming, searching, and ordering items in JavaScript arrays.

TL;DR

  1. Use push(), pop(), shift(), and unshift() to add or remove items.
  2. Use map(), filter(), and reduce() to transform array data.
  3. Use find(), some(), and includes() to search arrays.

Add and Remove

    push()

    Adds one or more items to the end, returns the new length.

    const cart = ['apple'];
    cart.push('banana'); // 2
    pop()

    Removes and returns the last item.

    const stack = ['init', 'save', 'close'];
    stack.pop(); // 'close'
    shift()

    Removes and returns the first item.

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

    Inserts, removes, or replaces items at any index. Mutates in place.

    const list = ['a', 'b', 'c'];
    list.splice(1, 1, 'x'); // returns ['b']
    // list is now ['a', 'x', 'c']
    unshift()

    Adds one or more items to the beginning, returns the new length.

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

Copy and Combine

    concat()

    Merges two or more arrays into a new array without mutation.

    const arr1 = [1, 2];
    arr1.concat([3, 4]); // [1, 2, 3, 4]
    spread operator

    Copies or combines arrays cleanly without mutation.

    const a = [1, 2];
    const b = [...a, 3];               // [1, 2, 3]
    const merged = [...a, ...b, ...c]; // all combined
    slice()

    Copies a whole array or a portion without mutating the source.

    const arr = [1, 2, 3, 4];
    arr.slice(1, 3); // [2, 3]
    Array.of()

    Builds a new array from arguments, even a single number.

    Array.of(7); // [7]
    Array(7);    // empty array with length 7
    structuredClone()

    Deep-copies an array, including nested objects, with no shared references.

    const original = [{id: 1}];
    const copy = structuredClone(original);

Transform Data

    map()

    Creates a new array by transforming every element with a function.

    const prices = [10, 20, 30];
    prices.map(p => p * 1.1); // [11, 22, 33]
    filter()

    Returns a new array keeping only items that pass the test.

    const users = [{active: true}, {active: false}];
    users.filter(u => u.active); // [{active: true}]
    reduce()

    Combines all items into a single value; always pass an initial value.

    const nums = [1, 2, 3];
    nums.reduce((acc, n) => acc + n, 0); // 6
    toSorted()

    Returns a new sorted array; the original is left unchanged.

    const nums = [3, 1, 2];
    nums.toSorted(); // [1, 2, 3], nums unchanged
    toReversed()

    Returns a new reversed array; the original is left unchanged.

    const nums = [1, 2, 3];
    nums.toReversed(); // [3, 2, 1], nums unchanged

Search, Order, and Flatten

    find()

    Returns the first element matching a condition, or undefined.

    users.find(u => u.id === 1); // {id: 1, ...}
    includes()

    Returns true if the exact value exists in the array.

    ['read', 'write'].includes('write'); // true
    some()

    Returns true if at least one element passes the test.

    users.some(u => u.isAdmin); // true or false
    every()

    Returns true only if all elements pass the test.

    tasks.every(t => t.done); // true or false
    findIndex()

    Returns the index of the first element matching a condition, or -1.

    users.findIndex(u => u.id === 1); // 0
    sort()

    Sorts the array in place; always pass a comparator for reliable results.

    names.sort((a, b) => a.localeCompare(b));
    reverse()

    Reverses the array in place; copy first if you need the original order.

    const nums = [1, 2, 3];
    nums.reverse(); // [3, 2, 1], mutates nums
    join()

    Combines all elements into a string with a separator.

    ['a', 'b', 'c'].join(','); // "a,b,c"
    forEach()

    Runs a function on each item for side effects; returns undefined.

    items.forEach(item => console.log(item));
    Array.from()

    Converts any iterable — string, Set, NodeList — into a real array.

    Array.from('hi');             // ['h', 'i']
    Array.from(new Set([1,1,2])); // [1, 2]
    flat()

    Collapses nested arrays one level deep; pass a depth to go deeper.

    const nested = [1, [2, [3]]];
    nested.flat();   // [1, 2, [3]]
    nested.flat(2);  // [1, 2, 3]
    flatMap()

    Maps over items and flattens the result one level in a single pass.

    const words = ['hi there', 'foo bar'];
    words.flatMap(s => s.split(' '));
    // ['hi', 'there', 'foo', 'bar']
    at()

    Accesses an element by index; negative values count from the end.

    const arr = [10, 20, 30, 40];
    arr.at(0);  // 10
    arr.at(-1); // 40

Tips

  1. Prefer non-mutating methods like map, filter, and slice when you want to keep your original array unchanged.
  2. Chain map, filter, and reduce together to transform data in one readable pipeline instead of writing several separate loops.

Warnings

  1. Sort and reverse change the original array in place, so copy first if you need to keep the source order.
  2. splice() also mutates the original array, and the wrong delete count can silently remove items you meant to keep.

In Practice

FAQ