Skip to main content

Command Palette

Search for a command to run...

Array Methods You Must Know

Updated
•3 min read•View as Markdown
Array Methods You Must Know
D

I am a developer learning web development , I am a college dropout pursuing my passion in software field

Arrays are the bread and butter of data storage in JavaScript, and knowing how to manipulate them efficiently will save you hours of manual work.

Let’s dive into the essential methods every developer should have in their toolkit.


1. Adding and Removing Elements (The Stack Methods)

push() and pop()

These methods work at the end of an array. Think of a stack of plates: you add one to the top and take one off the top.

  • push(): Adds an element to the end.

  • pop(): Removes the last element.

Example:

JavaScript

let snacks = ["Apple", "Banana"];

snacks.push("Cookie"); 
// State: ["Apple", "Banana", "Cookie"]

snacks.pop(); 
// State: ["Apple", "Banana"]

shift() and unshift()

These work at the beginning of the array.

  • unshift(): Adds an element to the front (pushing everything else back).

  • shift(): Removes the first element.

Example:

JavaScript

let queue = ["User1", "User2"];

queue.unshift("VIP_User"); 
// State: ["VIP_User", "User1", "User2"]

queue.shift(); 
// State: ["User1", "User2"]

2. The Iterators: forEach() vs. The Traditional Loop

Before these methods existed, we used the for loop. While powerful, it requires a lot of "boilerplate" code (setting up a counter, checking length, etc.).

Traditional Loop:

JavaScript

let numbers = [1, 2, 3];
for (let i = 0; i < numbers.length; i++) {
    console.log(numbers[i] * 2);
}

forEach()

This method is a cleaner way to "do something" with every item in an array. It doesn't return anything; it just executes a function for each element.

Example:

JavaScript

let friends = ["Alice", "Bob"];

friends.forEach(name => {
    console.log("Hello " + name);
});
// Output: "Hello Alice", "Hello Bob"

3. Transformation: map()

map() is one of the most used methods in modern frameworks like React. It creates a new array by applying a function to every element of the original array.

Key Difference: Unlike forEach, map returns a brand new array and leaves the original one untouched.

Example:

JavaScript

let prices = [10, 20, 30];
let taxedPrices = prices.map(price => price * 1.1);

// Before: [10, 20, 30]
// After:  [11, 22, 33]

4. Selection: filter()

Need to find specific items? filter() creates a new array containing only the elements that pass a specific "test."

Example:

JavaScript

let scores = [45, 80, 60, 90, 30];
let passingScores = scores.filter(score => score >= 60);

// Before: [45, 80, 60, 90, 30]
// After:  [80, 60, 90]

5. The Powerhouse: reduce()

reduce() can feel intimidating, but think of it as a snowball rolling down a hill. It takes all the items in an array and "reduces" them into a single value (like a sum or a total string).

It takes two main arguments:

  1. The Accumulator (the running total/snowball).

  2. The Current Value (the item currently being processed).

Example (Finding the Sum):

JavaScript

let cartTotal = [5, 15, 10];

let total = cartTotal.reduce((accumulator, current) => {
    return accumulator + current;
}, 0); // 0 is the starting value

// Result: 30
2 views