Skip to main content

Command Palette

Search for a command to run...

Javascript Array Methods

Popular Es6 javscript array methods

Updated
4 min readView as Markdown
Javascript Array Methods
D

Every great bio starts with a story. I believe that I'm more of an experience. I am a Developer Relations Engineer with knowledge of the Frontend:HTML, CSS, React, Bootstrap and TailwindCSS; Backend: Solidity, ExpressJs, Nodejs. I am looking to transition to fully in the Developer Relations space.

Hello and welcome!! 🤩🤩🤩

In this article, we will be looking at four of the modern Javascript Es6 array methods: map(), forEach(), filter(), reduce() methods.

What are methods in Javascript?

JavaScript methods are incredibly useful for performing various actions on objects. Essentially, a JavaScript method is a property that holds a function definition, which makes it easy to work with objects programmatically. Whether you're manipulating the DOM, handling user input, or performing complex calculations, JavaScript methods provide a powerful and flexible toolset for developers to work with. So if you're looking to take your web development skills to the next level, mastering JavaScript methods is a smart move!

The Javascript map() method

The .map method allows you to iterate through an array and carry out a specific function call for each of the array items. This will create a new array in which the changed array items are contained.

The general syntax for the map method is:

array.map(function to be carried out on each item)

//map method with a function call 
const numbers = [1, 2, 3, 4, 5];
function double(x) {
  return x*2;
};
const newNumbersArray = numbers.map(double);
console.log(newNumbersArray);

//console output = [2, 4, 6, 8, 10]

//map method with an annonymous function call 
const anotherNumbersArray = numbers.map((x) => {return x*3});
console.log(anotherNumbersArray);
//console output = [2, 4, 6, 8, 10]

In the codepen above, there are two examples of how the map method works.

In the first example, we defined a function outside the map method. The function we defined takes in the command we want to happen to each of the items in the array. The second example sees us using an arrow function to define our function call directly inside the method.

Javascript forEach() method

The forEach method calls a function for each element in an array.

The forEach method is similar to the map method in that it iterates over the items in an array with a function.

However, the difference between a forEach method and a map method is that a map method returns a new array of the modified array items while a forEach method returns undefined.

We use a forEach method when we have a small array to iterate over, as it is faster than the map method.

const numbers = [1, 2, 3, 4, 5, 6, 7];
const newArray = [];
 numbers.forEach((x) => {newArray.push(x*2)})
console.log(newArray);
//console output = [2, 4, 6, 8, 10, 12, 14]

You will notice in the example above that we had to create a new array ourselves and add our updated list items to it.

Javascript filter() method

What the filter method does is that it returns, in a new array, the array items that satisfy/meet the condition embedded in the function.

const numbers = [10, 2, 13, 4, 50, 60, 7];
const newArray = numbers.filter((x) => {return x > 10});
console.log(newArray);

// console output = [13, 50, 60]

We see that in the new array newArray that the numbers returned are the ones that satisfy the condition in the function. That is, it returned numbers that were greater than 10.

Javascript reduce() method

The reduce() method executes a reducer function for an array element. The reduce() method returns a single value: the function's accumulated result. The reduce() method does not execute the function for empty array elements. The reduce() method does not change the original array.

const numbers = [10, 2, 13, 4, 50, 60, 7];
numbers.reduce((total, num) => {return total * num})
// output = 21840000

The reduce method takes two required parameters, total and currentValue.

The reduce method does a scalar operation on the array, which is a vector. In a scalar operation (addition, multiplication, subtraction, or division) of a vector (array), only one value is returned.

Think of the total parameter as the present value or the current array. The current value is the individual values in the array.

We can now determine the scalar operation we want to happen on the array or vector. In the case of the example above, each item on the array, starting from index 0 is multiplied by each other to make one single unit value.

That's the end of this article for today! Thanks for dropping by. I hope you had as much fun reading this as I did writing it.

Follow me Linkedin

This article was meant to be part of the #2Articles1Week challenge, but I missed the part where we were to include it in the articles themselves.

C

Great article!

A
Akash Raj3y ago

I think there is a mistake in the 1st code which you have provided which is for map() method. Where you have written code for anonymous fn call in the return statement you are multiplying current element of array with 3 so the output would be [ 3, 6, 9, 12, 15 ].

1
D

Hello 👋 Thank you for your observation. However, in the first code, the return statement had return x*2, which means that the elements in the array were multiplied by 2.

A
Akash Raj3y ago

But where you have used the arrow function it has x*3 as the return statement right, so it will print multiple of 3 in the new array.

C

Dumebi Okolo you actually wrote x*3 in the second example. The anotherNumbersArray constant. That's what he's trying to say.

S

Great content! Also, I've written an in-depth article about the 10 most powerful JavaScript array methods. I'd love it if you check out my article and also share it with others who will find it helpful! https://shivankjshacker.hashnode.dev/10-powerful-javascript-array-methods-you-need-to-know

1
D

I've read your article. Truly educational. I'd definitely be sharing with my network.

1
S

Dumebi Okolo Thanks so much!

A

Well done 😁

O

Great work!

1
D

Thank you! 😁