Skip to main content

JavaScript Array Methods You Must Know

Updated 6 min readMarkdown
JavaScript Array Methods You Must Know

Imagine this.

You are building your first real web application.

Maybe it's:

  • an e-commerce store

  • a student management system

  • a weather dashboard

  • or even a social media app

Your application suddenly starts dealing with lists of data everywhere:

  • List of users

  • List of products

  • List of notifications

  • List of orders

  • List of messages

And all of these lists are stored inside something called an array.

let users = ["Ali", "Ahmed", "Sara", "Ayesha"];

At first, working with arrays seems simple.

But then real problems start appearing:

  • Add a new user

  • Remove the last order

  • Filter only active users

  • Transform prices with discount

  • Calculate total revenue

Doing this with normal loops quickly becomes messy.

That’s when JavaScript Array Methods become your best friends.

These methods allow you to transform, filter, calculate, and manipulate data elegantly.

In this guide, we will explore the most powerful array methods every developer must know:

  • push()

  • pop()

  • shift()

  • unshift()

  • forEach()

  • map()

  • filter()

  • reduce()

And we will learn them through stories and real-world problems.


The Restaurant Story πŸ”

Imagine you own a restaurant management system.

Every customer order is stored in an array.

let orders = ["Burger", "Pizza", "Pasta"];

Your kitchen constantly needs to:

  • add new orders

  • remove completed orders

  • transform orders

  • calculate totals

Let's see how array methods help.


1. push() β€” Adding a New Order

A new customer just ordered Fries.

We need to add it to the order list.

let orders = ["Burger", "Pizza", "Pasta"];

orders.push("Fries");

console.log(orders);

Output:

["Burger", "Pizza", "Pasta", "Fries"]

What Happened?

push() added the item to the end of the array.

Before

["Burger", "Pizza", "Pasta"]

After push()

["Burger", "Pizza", "Pasta", "Fries"]

Real World Use Cases

push() is used everywhere:

  • Adding items to a shopping cart

  • Adding new notifications

  • Adding new chat messages

  • Adding a new user to a list


2. pop() β€” Removing the Last Order

Now imagine the last order was cancelled.

We need to remove it.

let orders = ["Burger", "Pizza", "Pasta"];

orders.pop();

console.log(orders);

Output:

["Burger", "Pizza"]

Before

["Burger", "Pizza", "Pasta"]

After pop()

["Burger", "Pizza"]

pop() removes the last element of the array.

It is commonly used when dealing with:

  • stacks

  • undo operations

  • history tracking


3. shift() β€” Removing the First Order

Restaurants usually process the first order first.

So once an order is completed, we remove the first item.

let orders = ["Burger", "Pizza", "Pasta"];

orders.shift();

console.log(orders);

Output

["Pizza", "Pasta"]

Before

["Burger", "Pizza", "Pasta"]

After shift()

["Pizza", "Pasta"]

4. unshift() β€” Adding Priority Order

Sometimes a VIP customer arrives.

Their order must go to the front.

let orders = ["Burger", "Pizza"];

orders.unshift("VIP Steak");

console.log(orders);

Output

["VIP Steak", "Burger", "Pizza"]

Before

["Burger", "Pizza"]

After unshift()

["VIP Steak", "Burger", "Pizza"]

The Real Power Begins Now

So far we learned methods that add or remove elements.

But modern JavaScript applications need more powerful tools.

Now we enter the world of functional array methods.

These are heavily used in:

  • React

  • Next.js

  • Node.js

  • Data processing

  • APIs


5. forEach() β€” Visit Every Item

Imagine you want to print every order.

Instead of writing loops manually, we use forEach().

let orders = ["Burger", "Pizza", "Pasta"];

orders.forEach(function(order){
  console.log(order);
});

Output

Burger
Pizza
Pasta

Arrow Function Version

orders.forEach(order => console.log(order));

Traditional Loop vs forEach()

Traditional Loop

for(let i = 0; i < orders.length; i++){
 console.log(orders[i]);
}

Using forEach

orders.forEach(order => console.log(order));

forEach() is:

  • cleaner

  • more readable

  • easier to maintain


6. map() β€” Transforming Data

Now imagine your restaurant app stores prices in dollars, but you want to convert them to rupees.

let prices = [10, 20, 30];

Using map():

let rupees = prices.map(function(price){
 return price * 280;
});

console.log(rupees);

Output

[2800, 5600, 8400]

Important Concept

map() creates a new array.

Original array stays unchanged.

Before

[10,20,30]

After map()

[2800,5600,8400]

map() Flow Diagram

Original Array
[1,2,3,4]

   |
   v

Transformation
num * 2

   |
   v

New Array
[2,4,6,8]

Real Example with Objects

Imagine users in your app.

let users = [
 {name:"Ali", age:20},
 {name:"Sara", age:25},
 {name:"Ahmed", age:30}
];

Extract only names.

let names = users.map(user => user.name);

console.log(names);

Output

["Ali", "Sara", "Ahmed"]

7. filter() β€” Selecting Specific Data

Now imagine you only want adult users.

let users = [
 {name:"Ali", age:16},
 {name:"Sara", age:22},
 {name:"Ahmed", age:30}
];

Using filter():

let adults = users.filter(user => user.age >= 18);

console.log(adults);

Output

[
 {name:"Sara", age:22},
 {name:"Ahmed", age:30}
]

filter() Logic

Array
[5,10,15,20]

Condition
num > 10

Check Each Value

5   ❌
10  ❌
15  βœ…
20  βœ…

Result
[15,20]

map() vs filter()

Method Purpose
map() Transform data
filter() Select data

Example:

map β†’ change values
filter β†’ remove values

8. reduce() β€” Turning Many Values into One

Now imagine your restaurant wants to calculate total revenue.

let orders = [100, 200, 300, 400];

Using reduce():

let total = orders.reduce(function(sum, price){
 return sum + price;
}, 0);

console.log(total);

Output

1000

reduce() Step-by-Step

Array
[100,200,300]

Start sum = 0

Step 1
0 + 100 = 100

Step 2
100 + 200 = 300

Step 3
300 + 300 = 600

Final Result

600

reduce() Visual

[1,2,3,4]

0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10

Final Result β†’ 10

Why Modern Developers Love These Methods

These methods make code:

Shorter

numbers.map(n => n*2)

Cleaner

users.filter(user => user.age > 18)

Easier to Understand

orders.reduce((total, price) => total + price, 0)

Instead of writing long loops.

No comments yet. Be the first to comment.