15 Important JavaScript Array Methods You Should Know
Use these methods to improve your JavaScript game 😎
You will probably use arrays in every application you ever build. It has several methods, some of which are very confusing. I’ve made a list of 15 array methods you should master since they come in handy often.
An important thing about array methods is that some are mutable and some are immutable. It’s essential to keep this in mind when deciding which method to use for a particular problem.
Most of the array methods in this list take a similar callback as the argument. The first argument is the current item, the second argument is the index and the third is the entire array. Now we’ve got this out of the way, let’s get started with the list:
1. ForEach
Loops over each element in an array and executes a callback function.
2. Map
Loops over each element in an array and executes a callback function. Creates a new array with the return values of the callback function.
3. Filter
Loops over each element in an array and selects only those elements which are true to a condition. A new array is returned, based on the selected elements.
4. Find
Finds the first element in the array that fulfils a condition. If none are found, undefined
will be returned.
5. FindIndex
Similar to the previous method, it returns the index of the first element that fulfils a given condition. If none are found, -1
is returned.
6. Reduce
This is an advanced method, that can be used to combine the elements of an array. The main difference is that the callback takes an accumulator as the first argument. The return value of the callback becomes the accumulator in the next iteration.
7. Every
This method takes a callback that returns a boolean value. Every() will return true if the condition is valid for ALL the elements in the array.
8. Some
Like the previous method, this one also takes a callback that returns a boolean. Some() will return true if the condition is valid for AT LEAST one element.
9. Sort
This is a method used to sort the elements in an array. It sorts the array in ascending order by default. It takes a callback function, with two elements — a
and b
. If a
is less than b
, then return -1
, else return 1
. If they are equal, return 0
.
Keep in mind that unlike other array methods,
sort
mutates the array.
10. Flat
Flat is used to flatten nested array into a single array. You can specify a depth up to which the array will be flattened.
11. Reverse
Reverses the order of elements in an array.
12. Include
This method returns true
if an element is present in an array.
13. Fill
The fill method sets the elements of the array to a given value. I like to use this method when I want to use the map/forEach method a specific number of times.
14. At
This method returns the element of a given index. The difference between this and the traditional way of accessing(i.e. arr[1]
) an element is that it supports negative indices too.
15. Concat
This method is used to combine two arrays.
That’s the end of the post! If you liked it, leave a clap(or two) and follow me so as not to miss any more posts. Follow me on Twitter for daily content on Web development, freelancing and web3. Bye for now!