8 Must-Know Array Methods in JavaScript

These array methods will help you write clean code and reduce your coding time considerably.

Ruhul Amin
4 min readApr 19, 2022

We, JavaScript programmers, have to write a lot of code with an array and calculate the value from the array. Today I will discuss 8 awesome array methods that can help you write clean code. So let's start.

1. filter() Method:

The filter() method basically makes a new array containing sub-elements from the original array based on some condition. For example, we have a cities array and we want to separate some cities whose population is greater than 11000000. So how we can do it? We can do it by using a loop, right?

Excuse me! Do you have enough time to do it? JavaScript has an awesome array method name filter(). We can do this same task by using filter() easily.

2. map() Method:

The map() method is another smart array method in JavaScript. We can easily iterate over an array and make a sub-array using the map. For example, if we want to make a sub-array from cities array by taking their names only, we can easily do it by using the map() method.

So what is the difference between filter() and map()? 🤔 The map() method returns the same number of elements that are present in the original array but the value can be changed. On the other hand, filter() returns less or the same number of elements in the original array but the value can not be changed.

3. find() Method:

We can easily find a single item from an array using the find() method. this method only returns the first matching item depending on true or false.

4. some() Method:

The some() method is almost similar to the find() method. The difference between find() and some() is that find() returns an item from an array depending on the condition but some() returns just true or false depending on the condition. We can see an example for our better understanding:

5. every() Method:

every() and some() both return true/false depending on conditions. But the difference is some() returns true instantly if the condition is fulfilled without checking the rest of the items of the array. On the other hand, every() checks every element of the array and if any item doesn’t fulfill the condition it returns false. Here is an example:

6. reduce() Method:

The reduce() method mainly iterates over an array of items and gives us the accumulated values. For example, we want to get the total population of all cities, as shown below:

7. includes() Method:

The includes() method mainly checks whether the desired item exists in the array or not. If the item exists then it returns true otherwise it will return false.

8. forEach() Method:

The forEach() method is like a for loop in JavaScript but it doesn’t return a new array at all. It can only iterate over all items of an array. I am not giving an example of this. Make an example by yourself. 🤣

Thanks for reading my post. If you have any questions related to this post or this topic, do raise your questions in Counsily, where you can talk to verified experts directly and get answers. Counsily is a club for high intellect individuals to ask their questions & get personalized help from verified consultants. ask.counsily.com

Say hello on LinkedIn and Fiverr.

Happy Coding. 💕💖

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

--

--