The 7 JS Array Methods you will need in 2021
Get a great start off your year learning these useful methods
JavaScript gives us a TON of different methods for dealing with arrays. We’ll go through the 7 essential ones for you to improve your JS development skills in just a couple of minutes.
1. Array.map()
Whenever you use the .map()
method on an array, it will create a new, modified version of the original array. The .map()
method takes a function that allows you to loop through all the items in the array and modify them.
The .map()
method will come in handy whenever you want to update all the items in an array and store it in a new one.
Let’s say we have an array containing car brands:
const cars = ["Porsche", "Audi", "BMW", "Volkswagen"];
Of course, we think all of the cars are pretty cool and want to add some text expressing that. We can do this using the .map() method:
const coolCars = cars.map(car => `${car} is a pretty cool car brand!`);// Result:
["Porsche is a pretty cool car brand!", "Audi is a pretty cool car brand!", "BMW is a pretty cool car brand!", "Volkswagen is a pretty cool car brand!"];