Skip to main content

Command Palette

Search for a command to run...

Understanding Important Array Methods in JavaScript

Updated
4 min readView as Markdown

Arrays are one of the most commonly used parts of JavaScript.

Whenever we work with:

  • lists of users

  • products

  • tasks

  • marks

  • API data

we usually use arrays.

JavaScript provides built-in methods that make working with arrays much easier.

At first, many beginners try solving everything using loops.

That works, but modern JavaScript methods are cleaner, shorter, and easier to read.


Why Array Methods Matter

Imagine managing data manually using loops every time.

Example:

let numbers = [1, 2, 3]

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

This works, but modern array methods simplify common operations like:

  • adding values

  • removing values

  • transforming data

  • filtering data

  • calculating total

These methods are heavily used in real-world JavaScript.


push() and pop()

These methods work at the end of the array.


push()

push() adds a value to the end.

Example:

let fruits = ["Apple", "Mango"]

fruits.push("Banana")

console.log(fruits)

Before:

["Apple", "Mango"]

After:

["Apple", "Mango", "Banana"]

pop()

pop() removes the last value.

Example:

let fruits = ["Apple", "Mango", "Banana"]

fruits.pop()

console.log(fruits)

After:

["Apple", "Mango"]

shift() and unshift()

These methods work at the start of the array.


unshift()

Adds value at the beginning.

let numbers = [2, 3]

numbers.unshift(1)

console.log(numbers)

Output:

[1, 2, 3]

shift()

Removes the first value.

let numbers = [1, 2, 3]

numbers.shift()

console.log(numbers)

Output:

[2, 3]

Traditional Loop vs Modern Methods

Before methods like map() and filter(), developers mostly used loops.

Example:

let numbers = [1, 2, 3]
let result = []

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

console.log(result)

This works, but JavaScript introduced cleaner methods.


map()

map() creates a new array by transforming each element.

Example:

let numbers = [1, 2, 3]

let doubled = numbers.map(num => num * 2)

console.log(doubled)

Output:

[2, 4, 6]

Original array:

[1, 2, 3]

New array:

[2, 4, 6]

The original array stays unchanged.


filter()

filter() creates a new array containing only matching elements.

Example:

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

let result = numbers.filter(num => num > 10)

console.log(result)

Output:

[15, 20]

It filters out values that do not match the condition.


forEach()

forEach() is mainly used to run something for every element.

Example:

let fruits = ["Apple", "Mango", "Banana"]

fruits.forEach(fruit => {
  console.log(fruit)
})

Output:

Apple
Mango
Banana

Unlike map(), it does not create a new array.


reduce()

reduce() is used to combine array values into a single value.

This sounds complicated at first, but the idea is simple.

Example:

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

let total = numbers.reduce((acc, num) => {
  return acc + num
}, 0)

console.log(total)

Output:

10

Here:

  • acc = accumulated value

  • numbers keep getting added

Step-by-step:

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

Real-World Thinking

These methods are everywhere in modern JavaScript.

Examples:

  • filtering products

  • transforming API data

  • calculating totals

  • displaying lists in React

Understanding them properly is extremely important.


How to Think About These Methods

  • map() → transform values

  • filter() → select values

  • reduce() → combine values

  • forEach() → run code for every value

Once this mental model becomes clear, arrays become much easier to work with.


One Line Summary

Array methods simplify common operations like adding, removing, transforming, filtering, and combining data.


Conclusion

JavaScript array methods make code shorter, cleaner, and easier to understand compared to traditional loops.

Instead of manually handling everything, these methods provide structured ways to work with data efficiently.

Learning them properly is important because they are heavily used in modern JavaScript development.