Skip to main content

Command Palette

Search for a command to run...

Array Methods You Must Know (JavaScript)

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

Computer Engineering student | Full-Stack Developer (in progress) | DSA in C++ | AI-era Tech Explorer 🚀 Building skills, mindset, and systems to create impactful technology.

When you start JavaScript, arrays look very simple.
But after some time you realize most real work happens inside arrays — looping, transforming data, filtering values, calculating totals.

And instead of writing long loops every time, JavaScript gives built-in array methods which make life much easier.

Let’s go through the important ones every beginner should know.


push() and pop() → add / remove from end

Think of push and pop like a stack.

push()

Adds item at the end.

Example:

Before → [10, 20, 30]
After push(40) → [10, 20, 30, 40]

Useful when you want to keep adding new data.


pop()

Removes last item.

Before → [10, 20, 30]
After pop() → [10, 20]

Pop also returns the removed value.


shift() and unshift() → start of array

These are similar, but work at the beginning.

unshift()

Adds item at start.

Before → [20, 30]
After unshift(10) → [10, 20, 30]


shift()

Removes first item.

Before → [10, 20, 30]
After shift() → [20, 30]


forEach() → simple iteration

forEach is used when you just want to run something for each item.

Example idea:
Print each number in array.

It does not return a new array.
It is mainly for side effects like logging or updating UI.

So if you need transformation, forEach is not the best choice.


map() → transform each element

map creates a new array by changing each item.

Example:
Numbers → [2, 4, 6]
Double them using map → [4, 8, 12]

Why not use a for loop?

You can use a loop, but map is cleaner and shorter.
It also avoids manually pushing values into a new array.

Think of map as:
“Take each value → change it → return new array”


filter() → select specific values

filter is used when you only want some items.

Example:
Numbers → [5, 12, 8, 20]
Filter numbers > 10 → [12, 20]

filter does not change original array.
It creates a new array with matching values.

So map = change values
filter = select values


reduce() → combine values into one result

reduce feels confusing at first, but idea is simple.

reduce takes many values and turns them into one final value.

Example:
Find sum of [10, 20, 30]

Result → 60

reduce keeps an accumulator which stores the running result.

You don’t need to master reduce now, just remember:
reduce = accumulate / combine


Small comparison (loop vs map/filter)

Traditional loop:

  • more lines

  • manual array handling

  • easy to make mistakes

map / filter:

  • shorter

  • readable

  • intention is clear

That’s why modern JS prefers array methods.