Categories
Web Development

JS Array Methods Cheatsheet

Handling collections of data efficiently is a core requirement of modern JavaScript engineering. While standard loops work, ECMAScript array methods provide highly expressive, declarative ways to mutate, filter, and transform datasets.

This reference handbook covers the most vital JavaScript array methods you need for clean production-grade code, broken down by execution type.

1. The Heavy Hitters (Transformation & Filtering)

These methods are non-mutating; they return a brand-new array without altering the original dataset configuration.

.map()

Transforms every single element inside an array and returns a new array of identical length.

JavaScript

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

.filter()

Evaluates every element against a conditional test. Returns a new array containing only elements that evaluate to true.

JavaScript

const scores = [45, 80, 92, 60];
const passing = scores.filter(score => score >= 70); // [80, 92]

.reduce()

Executes a user-supplied callback accumulator function across all elements, reducing the array down to a single output value (e.g., an object, string, or number).

JavaScript

const expenses = [100, 250, 50];
const total = expenses.reduce((acc, current) => acc + current, 0); // 400

2. Search & Verification Utilities

Methods designed to find specific values or verify structural criteria inside an array.

.find()

Returns the first item that matches your specified conditional criteria. If no matching block is found, it returns undefined.

JavaScript

const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
const targetUser = users.find(user => user.id === 2); // {id: 2, name: 'Bob'}

.includes()

Returns a simple boolean (true/false) stating whether a target primitive element exists within the collection.

JavaScript

const techStack = ['React', 'Node', 'MongoDB'];
const hasNextJS = techStack.includes('Next.js'); // false

3. Structural Mutators (Alters the Original Array)

Warning: These methods modify the original array in-place. Use with caution in state-managed applications like React.

  • .push(): Appends an element to the absolute end of the array.
  • .pop(): Removes the very last element of the array and returns it.
  • .shift(): Strips the first element out of the array layout.
  • .unshift(): Prepends a new item directly to the front of the array index.
    What is the difference between .map() and .forEach() in JavaScript?

    The .map() method transforms elements and returns an entirely new array of the same length without altering the source data. The .forEach() method simply executes a callback loop function on each item in-place and returns undefined.

    Does JavaScript .filter() modify the original array?

    No, .filter() is an immutable array method. It creates a shallow copy of a portion of the given array, filtered down to just the elements that pass the structural condition test.

    Leave a Reply

    Your email address will not be published. Required fields are marked *