A quick set of questions beginners can ask to know which javascript array method to use!
Tags: javascript, beginners
Published: 2020-02-28 15:54:02


## This is a quick one

As a beginner, one of the first tasks you must learn is to ask "what am I trying to achieve"? I found these simple questions helped me choose the correct array method for the job.

## The Questions

* Do I want only some of these items? [Array.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

* Do I want to convert each item into something else? [Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

* Do I want to run some code for every item in the array? [Array.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)

* Do I want to know if at least one item in an array matches some criteria? [Array.some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)

* Do I want to know if every item in an array matches some criteria [Array.every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)

* Do I want to get the first item from an array which matches some criteria? [Array.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

* Do I want to know if an item is in an array? [Array.includes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)

* Do I want to know the index of an item in an array? [Array.indexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)

* Do I want to sort the array items in a particular order? [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)

* Do I want to reverse the order of the items in the array? [Array.reverse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)

* Do I want only some of the array and I know the indexes? [Array.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)

* Do I want the first or last "n" items in array? [Array.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)

* Do I want to join the items into a string? [Array.join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

* Do I want to add 1+ items to the start of an array? [Array.unshift](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift)

* Do I want to add 1+ items to the end of the array? [Array.push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)

* Do I want to remove 1 item from the start of an array? [Array.shift](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift)

* Do I want to remove 1 item from the end of the array? [Array.pop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop)

* Do I want to join 2+ arrays together? [Array.concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat)

## Summary

There are some notes to the above;

1. These are nowhere near the complete list of Array methods but are the ones I use most in my day-to-day work.

2. These methods are powerful and can not only to answer these simple questions but can do a lot more as well! But beware, some methods can also have unintended side effects.

3. I've not included Array.reduce here but it's the one I use if I want to combine some of the above questions. It's also useful if I want  more control over the output.

4. Don't worry if you don't remember even half of these. I always have to re-visit documentation to remember how to use them!

5. These methods are like a set of tools in a toolbox. Sometimes a range of tools can complete a task but the more tools you have, the better chance you can use the best one. Also, don't forget that you can combine tools (array methods) to achieve your goal. Composition is powerful!

These questions can help let you develop a sense of what methods are useful in which situations. Most the time it's a combination of the above methods enable the user to achieve their goal. For example,  using `filter` and `map` to remove and transform one set of items into another. Combining these methods reads nicely and it's easier to understand when revisiting the code in the future.
 
So that's it. A real quick post! Hopefully you found this useful :)

Which array methods do you use most? Are there any missing from the above that you can't live without?