Skip to main content

Command Palette

Search for a command to run...

Array Methods Every living beings should know

Updated
6 min read
Array Methods Every living beings should know

JavaScript Array Methods That Quietly Change the Way You Think About Data

If you’ve ever worked with arrays in JavaScript, you probably started with something like this:

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

It works. It’s clear.

But after a while, you realize something interesting.

JavaScript actually gives you much more expressive tools to work with arrays. Tools that let you describe what you want, instead of manually looping through everything.

Today, we’ll walk through some of the most useful ones:

  • push() and pop()

  • shift() and unshift()

  • map()

  • filter()

  • reduce() (beginner-friendly explanation)

  • forEach()

And while reading, I want you to open your browser console and try each example yourself. Trust me — it sticks better when you run it.

1. push() and pop()

Think of an array like a stack of books.

You always add a new book on top, and when removing one, you take the top book off.

That’s exactly how push() and pop() behave.

push() → Add an element to the end

let fruits = ["apple", "banana"];

fruits.push("mango");

Before

["apple", "banana"]

After

["apple", "banana", "mango"]

push() simply adds a new element at the end of the array.

pop() → Remove the last element

let fruits = ["apple", "banana", "mango"];

fruits.pop();

Before

["apple", "banana", "mango"]

After

["apple", "banana"]

It removes the last element from the array.

Quick Question for You

What do you think will happen here?

let arr = [1,2,3];
arr.push(4);
arr.pop();

Try it in the console.

Did the array change?

2. shift() and unshift()

Now imagine a line of people waiting for tickets.

The first person enters first.

Arrays also allow operations at the front.

shift() → Remove the first element

let queue = ["A", "B", "C"];

queue.shift();

Before

["A", "B", "C"]

After

["B", "C"]

The first element leaves the array.

unshift() → Add element at the beginning

let queue = ["B", "C"];

queue.unshift("A");

Before

["B", "C"]

After

["A", "B", "C"]

It adds an element to the front.

Small Insight

  • push() / pop() → work at the end

  • shift() / unshift() → work at the start

Simple pattern, easy to remember.

3. forEach()

Before jumping to map() and filter(), let’s understand forEach().

This method simply runs a function for every element in an array.

Example:

let numbers = [1,2,3];

numbers.forEach(function(num){
  console.log(num);
});

Output

1
2
3

It loops through the array, but unlike map() or filter(), it does not return a new array.

Think of it as:

“Go through each item and do something.”

Traditional for loop vs forEach

Traditional loop

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

forEach

numbers.forEach(num => console.log(num));

Both do the same thing, but forEach is cleaner and easier to read.

4. map()

Now we reach one of the most powerful methods in JavaScript.

map() is used when you want to transform every element of an array.

Imagine you have this:

[1,2,3,4]

And you want:

[2,4,6,8]

Instead of writing a loop, map() does it elegantly.

Example

let numbers = [1,2,3,4];

let doubled = numbers.map(function(num){
  return num * 2;
});

Before

[1,2,3,4]

After

[2,4,6,8]

Important thing:

map() creates a new array.

The original array stays unchanged.

How map works (conceptually)

Original array
      ↓
Apply function to each element
      ↓
Return new transformed array

You can imagine it like a machine where each value goes in and a new value comes out.

Compare with for loop

Using for loop

let result = [];

for(let i=0;i<numbers.length;i++){
  result.push(numbers[i]*2);
}

Using map

let result = numbers.map(num => num*2);

Much simpler.

5. filter()

Now suppose you don’t want to change values.

You want to remove some values based on a condition.

That’s where filter() shines.

Example

let numbers = [5,10,15,20];

let bigNumbers = numbers.filter(function(num){
  return num > 10;
});

Before

[5,10,15,20]

After

[15,20]

filter() keeps only the elements that satisfy the condition.

How filter works

Array → Check condition → Keep / Remove

Example flow:

5  → 5 > 10 ? -----remove
10 → 10 > 10 ? --- remove
15 → 15 > 10 ? --- keep
20 → 20 > 10 ? ----keep

Final array

[15,20]

6. reduce() (Beginner Friendly)

reduce() sounds scary at first, but the idea is simple.

It combines all array values into a single value.

Example: finding the sum of numbers.

Example

let numbers = [1,2,3,4];

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

Output

10

What is happening step by step?

Start with total = 0

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

Final result

10

So reduce() accumulates values step by step.

Visual idea

1 + 2 + 3 + 4
      ↓
     10

Everything reduces into one final value.

A Small Challenge For You

Open your browser console and try this.

let numbers = [5,10,15,20];

Task 1

Use map() to double each number.

Expected result

[10,20,30,40]

Task 2

Use filter() to get numbers greater than 10.

Expected result

[15,20]

Task 3

Use reduce() to calculate the total sum.

Expected result

50

One Last Thought

If you're new to JavaScript, methods like map(), filter(), and reduce() might feel unusual at first.

But once they click, something interesting happens.

You stop thinking in terms of:

“How do I loop through this array?”

And start thinking:

“What do I want to do with this data?”

And that shift is what makes modern JavaScript feel clean, expressive, and powerful.

If you're reading this, here’s something I'd suggest.

Open your console right now and experiment with:

map
filter
reduce

Break things. Try weird inputs. Change numbers.

Because the fastest way to understand code is not reading it — it's playing with it.