5 Must-Know JavaScript Array Methods You Should Know To Make Your Life Easier

Martin
3 min readOct 16, 2022

--

Introduction

Arrays are great for storing related data and are often used as a way of organising information. Most of us use them on a daily basis, but did you know that there are also some really neat array methods built into JavaScript?

These methods make our lives much easier, optimising what would otherwise be multiple lines of code into one simple command. Whether you’re just getting started with arrays or already feel like an expert, this article will help you become more efficient when working with them.

filter()

If you want to filter an array based on certain criteria, the filter() method might be what you need. This is a useful function that will return a new array with all the items you’re interested in.

It requires a function as a parameter, which will be called for each element in the array. If the function returns true, that element will remain in the array; otherwise, it will be removed from the array.

Example
We have requested data from the backend and would like to do client-side filtering based on a property that the array of objects has. In this case, we have requested jokes from JokeAPI and would like to filter the jokes where the category property equals Programming.

Sandbox link — https://playcode.io/982694

map()

The map() method transforms each item in an array, applying a function to it and storing the result in a new array, without actually changing the initial array.

Example
We have requested data from the backend and would like to extract info from that data. In this case, we have requested random user data from RandomDataAPI and would like to extract each person’s age into an array.

Sandbox link — https://playcode.io/982843

reduce()

The reduce() method reduces an array to a single value by applying a function on each element and accumulating the results. It’s a great method for finding a total or for finding the average of all items.

Example
We have an array that contains monthly deposits, and we would like to know the sum of all deposits.

Sandbox link — https://playcode.io/982850

some()

The some() method checks if at least one element in an array satisfies the test implemented by the provided function. If it does satisfy the test, it will return true; otherwise, it will return false.

Example
We have requested users from a backend and would like to know if one of them has been flagged as a bot.

Sandbox link — https://playcode.io/983403

every()

The every() method checks if every element in the array satisfies the test implemented by the provided function. If it does, it will return true; otherwise, it will return false

Example
We have a list of products in our cart and would like to check whether or not there is stock.

Sandbox link — https://playcode.io/986629

Conclusion

Arrays are one of the most basic and important data structures in any programming language. When learning JavaScript, it’s helpful to know how to use these array methods to manipulate and store data more efficiently. These methods include filter(), map(), reduce(), some(), and every() that can help make your code more efficient.

--

--