Sitemap
Kunal Arora

Kunal Arora

Mastering Array Methods in JavaScript: A Comprehensive Guide

4 min readJan 10, 2025

--

Array Properties and Methods:

Arrays have several properties and methods that help you manipulate and interact with the data. Some of these properties and methods are built into JavaScript, and you’ll find that they simplify a lot of your code.

Array methods are functions that can be called on an array to perform various operations like transforming, filtering, sorting, or aggregating elements in the array.

Some methods modify the original array (mutating), while others return new arrays or values (non-mutating).

Non-mutating Methods (Do not change the original array):

  1. map()
  2. filter()
  3. reduce()
  4. forEach()
  5. find()
  6. includes()
  7. slice()
  8. concat()
  9. some()
  10. every()

Mutating Methods (Modify the original array):

  1. push()
  2. pop()
  3. shift()
  4. unshift()
  5. splice()
  6. sort()

Detailed Explanation of Array Methods

Let’s explore both mutating and non-mutating methods in detail:

Non-mutating Methods

  • map()
    Purpose:
    Transforms each element in an array using a function and returns a new array.
    Example:
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8]

Explanation: The map() method iterates over each element in the array and applies the function num => num * 2. It returns a new array with the transformed elements.

  • filter()

Purpose: Filters elements based on a condition and returns a new array of only the elements that satisfy the condition.

Example:

const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4]

Explanation: The filter() method returns a new array containing only the even numbers from the original array.

  • reduce()

Purpose: Reduces the array to a single value by applying a function that accumulates the result over each iteration.

Example:

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // 10

Explanation: reduce() takes a callback function that receives two arguments: an accumulator (acc) and the current value (num). The result of the callback is accumulated into the accumulator, and the final result is returned.

  • forEach()

Purpose: Executes a given function once for each element in the array. It does not return a new array.

Example:

const numbers = [1, 2, 3, 4];
numbers.forEach(num => console.log(num)); // 1, 2, 3, 4 (printed separately)

Explanation: forEach() simply iterates over each element and executes the provided function for each element. It’s useful for side effects like logging or modifying external variables.

  • find()

Purpose: Returns the first element in the array that satisfies the provided condition.

Example:

const numbers = [1, 2, 3, 4, 5];
const found = numbers.find(num => num > 3);
console.log(found); // 4

Explanation: find() returns the first element greater than 3, which is 4.

  • includes()

Purpose: Checks if an array contains a specific element and returns a boolean (true or false).

Get Kunal Arora’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

const numbers = [1, 2, 3, 4, 5];
console.log(numbers.includes(3)); // true
console.log(numbers.includes(6)); // false

Explanation: includes() checks whether the number 3 is present in the array, returning true, and checks if 6 exists, returning false

  • slice()

Purpose: Creates a shallow copy of a portion of the array without modifying the original array.

Example:

const numbers = [1, 2, 3, 4, 5];
const sliced = numbers.slice(1, 4); // from index 1 to 3
console.log(sliced); // [2, 3, 4]

Explanation: slice() creates a new array from index 1 to index 3 (not including 4), which is [2, 3, 4].

  • concat()

Purpose: Combines multiple arrays into one new array.

Example:

const array1 = [1, 2];
const array2 = [3, 4];
const combined = array1.concat(array2);
console.log(combined); // [1, 2, 3, 4]

Explanation: concat() merges array1 and array2 into a new array.

  • some()

Purpose: Checks if at least one element in the array satisfies the provided condition, returning true if so, otherwise false.

Example:

const numbers = [1, 2, 3, 4, 5];
const hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven); // true

Explanation: some() returns true because there are even numbers in the array (e.g., 2, 4).

  • every()

Purpose: Checks if every element in the array satisfies the provided condition, returning true if so, otherwise false.

Example:

const numbers = [2, 4, 6];
const allEven = numbers.every(num => num % 2 === 0);
console.log(allEven); // true

Explanation: every() returns true because all the numbers in the array are even.

Mutating Methods

  • push()

Purpose: The push() method adds one or more elements to the end of the array, modifying the original array.

Example:

const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers); // [1, 2, 3, 4]

Explanation: push() adds 4 to the end of the array.

  • pop()

Purpose: The pop() method removes the last element from the array, modifying the original array.

Example:

const numbers = [1, 2, 3, 4];
numbers.pop();
console.log(numbers); // [1, 2, 3]

Explanation: pop() removes 4, the last element in the array.

  • shift()

Purpose: The shift() method removes the first element from the array, modifying the original array.

Example:

const numbers = [1, 2, 3, 4];
numbers.shift();
console.log(numbers); // [2, 3, 4]

Explanation: shift() removes 1, the first element in the array.

  • unshift()

Purpose: The unshift() method adds one or more elements to the beginning of the array, modifying the original array.

Example:

const numbers = [2, 3, 4];
numbers.unshift(1);
console.log(numbers); // [1, 2, 3, 4]

Explanation: unshift() adds 1 to the beginning of the array.

  • splice()

Purpose: The splice() method can add, remove, or replace elements in an array, modifying the original array.

Example:

const numbers = [1, 2, 3, 4];
numbers.splice(2, 1, 5, 6); // Removes 1 element at index 2, adds 5 and 6
console.log(numbers); // [1, 2, 5, 6, 4]

Explanation: splice() removes the element at index 2 and replaces it with 5 and 6.

  • sort()

Purpose: The sort() method sorts the elements of the array in place. It modifies the original array.

Example:

const numbers = [5, 3, 8, 1];
numbers.sort((a, b) => a - b); // Sorts in ascending order
console.log(numbers); // [1, 3, 5, 8]

Explanation: sort() sorts the elements numerically in ascending order.

--

--