Arrays are wonderful and a very particular bang out in JavaScript.

Today, were going to discuss 15 array methods every developer should know.

Each method will resolve a different problem, so its important were familiar with all of them.

15 useful methods to get the most out of JavaScript arrays

Thecallbackfunction will receive three arguments: the item, the index, and the full array.

Additionally, its possible to assign a value forthiswhen executing thecallbackby using the argumentthisArg.

Definition:

40% off TNW Conference!

Article image

How does reduce() work?

In our particular case, I did not provide an initial value.

And the final resulting value is20.

Article image

Similar to the other methods, thecallbackfunction will receive three arguments:currentValue,index, andarray.

Always be careful when usingmap().Remember that on each call will create a new array.

If you dont actually need the array and you are simply trying to iterate, useforEach()orfor-ofinstead.

Article image

By default, it will flatten one level.

Note that if we want to flatten all levels recursively, we can passInfinityas the argument of the function.

filter()

Together withmap()I think this is one of my favorites.

Article image

Thefilter()methodcreates a new arraywith all the elements that pass the test implemented by thecallbackfunction.

forEach()

TheforEach()method executes a provided function once for each array element.

So be very aware of this situation as it can lead to your code, producing unexpected results.

Article image

Otherwise, it returns -1, indicating that no element passed the test.

Unlike other methods,findIndex()will execute thecallbackfunction even for indexes with unassigned values.

find()

Thefind()method is similar to thefindIndex()method.

Article image

However, it returns thevalueof thefirstelement which satisfies the providedcallbackfunction as supposed to its index.

If no element satisfies thecallback, thenundefinedis returned.

The default sort order is ascending.

Article image

The complexity and performance of this method cannot be guaranteed, as it depends on the implementation.

The updates will happenin placeand will return a reference to the same array.

Note that the methodincludes()is case-sensitive when comparing strings and characters.

Article image

reverse()

Thereverse()method reverses an arrayin place.

This operation will mutate the original array and return a reference to the same.

It combinesflat()andmap()in one function.

Article image

Summary

JavaScript arrays come with some great methods that can simplify our development efforts.

Knowing them can save us time and, in some cases, even boost the performance of our code.

This article was originally published onLive Code StreambyJuan Cruz Martinez, founder, and publisher of Live Code Stream.

Article image

you’ve got the option to read this original piecehere.

Sign up for updates on everything related to programming, AI, and computer science in general.

Also tagged with

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image