Mastering Arrays: 3 Essential JavaScript Methods to Know

Big Zude
2 min readDec 17, 2022
Photo by Gabriel Heinzer on Unsplash

Learning about all the available array methods can be difficult for a JavaScript beginner. There are a few significant ones, though, that you should be sure to become acquainted with. These techniques will not only improve the efficiency of your code but also help you better comprehend and work with arrays in your programs.

.forEach

You can iterate through an array using the.forEach() method and take a certain action on each entry. Here is a more thorough illustration of the .forEach() function in action:

const names = ['John', 'Jane', 'James'];
names.forEach(name => { console.log(Hello, ${name}!);
});
// Output: Hello, John! Hello, Jane! Hello, James!

In this case, we want to print a greeting for each name in an array of names. We go through our array using the .forEach method, producing a greeting for each name as we go.

.map

The .map() method allows you to transform each element in an array into a new value. Here is a more detailed example of how to use the .map() method:

const numbers = [1, 2, 3, 4, 5];
const squaredNumbers = numbers.map(number => number * number);
console.log(squaredNumbers);
// Output: [1, 4, 9, 16, 25]

In this example, we have an array of numbers and we want to transform each number into its squared value. We use the .map() method to loop through our array and transform each element into its squared value.

.filter

The .filter() method allows you to filter out elements from an array based on a specific condition. Here is a more detailed example of how to use the .filter() method:

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const evenNumbers = numbers.filter(number => number % 2 === 0);
console.log(evenNumbers);
// Output: [2, 4, 6, 8, 10]

In this example, we have an array of numbers and we want to filter out all the even numbers. We use the .filter() method to loop through our array and only keep the elements that meet the condition of being even.

In general, the.forEach(), .map(), and .filter() methods are crucial array methods to understand in JavaScript since they let you quickly perform operations on all members in an array, change the values of elements, and filter out components according to particular criteria. Make sure to put these techniques to use in order to completely comprehend how they operate and how to apply them to your programming tasks.

--

--

Big Zude

React Frontend Engineer | Mobile Developer | UI Designer