Important JavaScript Array Methods Every Developer Should Know

Important JavaScript Array Methods Every Developer Should Know
August 15, 2023

Imagine you're a painter. Instead of creating every color from scratch, wouldn't it be fantastic if you had a palette full of vibrant shades ready to be used?

Similarly, JavaScript provides us with a palette of methods that make our coding life much simpler.

These built-in tools are like shortcuts that help us perform tasks without reinventing the wheel. Let's dive in and explore some of the most important ones.

1. forEach() - Looping Made Easy

This method works like a helper for arrays. Imagine arrays as a museum. If you go alone, you have to see each artwork by yourself.

But with a tour guide, they will show you each piece one by one. In the same way, instead of you having to look at each element in the array on your own, .forEach() helps you see each element step by step.

const fruits = ['apple', 'banana', 'cherry'];
fruits.forEach(fruit => {
  console.log(fruit);
});


2 .map() - Transforming Lists

Do you want to make a change to every single thing in a list (which we call an array)? Think of the .map() method like a special tool or a magic wand.

Just like a magic wand can change things in fairy tales, .map() helps you change every item in your list all at once.

const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]


3 .filter() - Picking What You Need

Imagine you have a big bowl of mixed items, and you want to keep only some of them. Think of this method like a strainer or a net.

When you pour the items through it, the ones you don't want get caught, and you're left with only the ones you want. That's how this works; it picks out and keeps the items from the list (array) you want and leaves behind the ones you don't need.

const ages = [16, 20, 12, 25, 30];
const adults = ages.filter(age => age >= 18);
console.log(adults); // [20, 25, 30]


4 .find() - Seek and You Shall Find

Imagine you have a box full of toys and you want to find the first blue toy in it. Instead of searching through all the toys one by one, think of .find() as a helper friend.

You tell this friend, "I want the first blue toy," and this helper quickly gives it to you. So, when you have a list (called an array) and you want to quickly get the first item that matches what you're looking for, you use .find() to help you out.

const books = ['Moby Dick', 'The Great Gatsby', '1984'];
const findGatsby = books.find(book => book === 'The Great Gatsby');
console.log(findGatsby); // 'The Great Gatsby'


5 .reduce() - Simplify Your Data

Imagine you have a magic pot. You put many things into it, like toys, candies, and coins. But instead of giving all those things back, this magic pot just gives you one special coin that represents everything inside it.

In the same way, this method takes all the items in your list (which we call an array) and combines them, or squishes them down, into just one single thing. So, it helps you take a big list and turn it into just one simple item.

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((total, num) => total + num, 0);
console.log(sum); // 10


6 .push() - Adding to the Array Party

Imagine you have a toy box and you want to put a new toy inside it. When you place that toy into the box, you usually put it on top of the other toys.

In the world of arrays (which are like lists of items in JavaScript), the .push() method is like adding a new toy to the top of your toy box. It lets you add new items to the end of your list.


const toys = ['doll', 'car'];
toys.push('ball');
// Now, the toys list has: ['doll', 'car', 'ball']


7 .pop() - Saying Goodbye to the Last Guest

Now, think about taking a toy out of the toy box. Usually, you'd take out the toy that's on the top, right?

The .pop() method does the same thing for arrays. It removes the last item from the list and also tells you which one it removed.

const toys = ['doll', 'car', 'ball'];
const removedToy = toys.pop();
// The removedToy will be 'ball', and the toys list now has: ['doll', 'car']


8 .shift() - Greeting the First Guest

But what if you wanted to take out the very first toy you put in the toy box, the one at the very bottom?

The .shift() method helps with that. It removes the first item from the list and shows you which one it took out.

const toys = ['doll', 'car', 'ball'];
const firstToy = toys.shift();
// The firstToy will be 'doll', and the toys list now has: ['car', 'ball']


9 .unshift() - Welcoming a VIP Guest First

Now, imagine you wanted to put a new toy at the very bottom of the toy box, pushing all the other toys up a bit.

The .unshift() method does that for arrays. It lets you add a new item to the very beginning of your list.

const toys = ['car', 'ball'];
toys.unshift('doll');
// Now, the toys list will start with 'doll' and look like: ['doll', 'car', 'ball']








Resources for You

ChatGPT Guide For Software Developers

Learn to use ChatGPT to stay ahead of competition

Front-End Developer Interview Kit

Today, Start preparing to get your dream job!

JavaScript Developer Kit

Start your JavaScript journey today!

Are you looking for Front-end Developer Job?

Get Front-end Interview Kit Now, And Be Prepared to Get Your Dream Job

Get Front-end Interview Kit

Newsletter for Developers!

Join our newsletter to get important Web Development and Technology Updates