10 Tips and Tricks to Work Fast With Array in JavaScript

Wanna speed up your works with arrays in JavaScript to save time? Check out these 10 Tips & Tricks which will literally speed up your coding.

Sadman Sakib Mugdho
The Startup
4 min readNov 2, 2020

--

Photo by Pakata Goh on Unsplash

JavaScripts comes with some amazing methods to play with the array. Let’s explore them…

1) map()

What you usually do when you have to iterate through every item in an array? Most probably you run for loop in that array. But there is a method built-in with JavaScript which is called map(). Here is an example of it.

The map method takes an argument which is every element of an array. Then it does some operations with that element and returns a new array.

2) filter()

The filter method is used when you have to filter some elements from an array if they pass the test implemented by the provided function. It is helpful when you have a large array and want to filter some items of the same category. Let’s see how it works…

In this case, the filter function is filtering the names which lengths are less than 6. That’s why we got Tom and Ema.

3) find()

Let’s say, you want to find a specific item from an array. Now you can go through the whole array and see if the item is there or not. But what if the array is populated with 1000 items? Here find method comes in the picture. Let's see how it works…

In this case, this find method is iterating through all the items in the array and checking if the item is less than 10.

NOTE: find() will return only one item

4) indexOf()

Let’s imagine you want to know the index number of an item inside an array. You can manually find it. But as programmers are lazy, JavaScript gives us a method called indexOf() to find the index of an item inside an array. Let’s see how it works…

5) join()

Now let’s think of a situation where you have to join every element of an array with a special character (or any character). JavaScript has a cool method called join() to join all the items of an array with some special characters. Here is an example…

Here the join function is iterating through each element of fruits array and joining them with a plus (+).

6) pop()

Now imagine you want to remove the last item of an array. What will you do? Don’t worry. JavaScript has a method called pop(). This method will pop up the last method. Here is an example…

7) push()

Last time removed the last item from an array. Now, what if you want to add an item to an array? Again, JavaScript has a method called push(). This method will push an element to an array. Here is an example…

8) shift()

In the last two cases, we removed and added an item at the last position of an array. Now, what if you want to remove the first element of an array? Here shift() method comes into the picture. Let’s see an example…

9) unshift()

Now we have learnt how to remove the first item from an array. Let’s learn how to add an item in the first position of an array. To add an item at the first position of an array, we use the unshift method(). Here is an example…

10) reverse()

Let’s say you want to reverse the items of an array. It means the first item will be the last and the last item will be the first. The second last item will be the second item and the second item will be the second last item and so on. In this case, we use the reverse() method. Here is an example…

Was this helpful for you? Let me know in the comments below. And if you have extra tips and tricks in your mind, then why are you waiting for? Let me know your tips and tricks so that I can add to the list!!!

--

--