DEV Community

Giovanni Galiero
Giovanni Galiero

Posted on

Mastering JavaScript Array Methods: Your Ultimate Guide — part 1 —

JavaScript arrays are a powerful tool for storing and manipulating data. But with power comes complexity, and it can be difficult to know which array method to use for a given task.
In this article, we’ll cover the top 5 JavaScript array methods so you can master them and take your coding skills to the next level.

array

1 .map()

The .map() method creates a new array by calling a provided function on every element in the original array. It's a great way to transform an array of data into a new form.

Here's an example:

_Example 1_
const numbers = [1, 2, 3, 4, 5]
// Use .map() to double each number
const doubledNumbers = numbers.map(num => num * 2)

console.log(doubledNumbers)
// Output: [2, 4, 6, 8, 10]

_Example 2_
const names = ['John', 'Jane', 'Jim']
// Use .map() to create an array of greetings
const greetings = names.map(name => `Hello, ${name}!`)

console.log(greetings)
// Output: ['Hello, John!', 'Hello, Jane!', 'Hello, Jim!']
Enter fullscreen mode Exit fullscreen mode

2 .filter()

The .filter() method creates a new array with all elements that pass the test implemented by the provided function. It's a great way to extract a subset of data from an array.

Here's an example:

_Example 1_
const numbers = [1, 2, 3, 4, 5]
// Use .filter() to get all even numbers
const evenNumbers = numbers.filter(num => num % 2 === 0)

console.log(evenNumbers) // Output: [2, 4]

_Example 2_
const words = ['dog', 'cat', 'elephant', 'bear', 'zebra'];
// Use .filter() to get all words with more than 3 letters
const longWords = words.filter(word => word.length > 3);

console.log(longWords); // Output: ['elephant', 'bear', 'zebra']
Enter fullscreen mode Exit fullscreen mode

3 .reduce()

The .reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce the array to a single value. It's a great way to perform operations on an array of data and return a single result.

Here's an example:

_Example 1_
const numbers = [1, 2, 3, 4, 5]
// Use .reduce() to find the sum of the numbers
const sum = numbers.reduce((acc, num) => acc + num, 0)

console.log(sum) // Output: 15

_Example 2_
const words = ['dog', 'cat', 'elephant', 'bear', 'zebra']
// Use .reduce() to concatenate all words into a sentence
const sentence = words.reduce((acc, word) => `${acc} ${word}`, '')

console.log(sentence) // Output: 'dog cat elephant bear zebra'
Enter fullscreen mode Exit fullscreen mode

4 .sort()

The .sort() method sorts the elements of an array in place and returns the sorted array. By default, it sorts elements in ascending order based on their Unicode code points.

The sort function can return:

  • a negative value if a should be sorted before b
  • zero (0) if a and b are equal
  • a positive value if a should be sorted after b

Here's an example:

_Example 1_
const numbers = [5, 2, 4, 1, 3]
// Use .sort() to sort the numbers in ascending order
const sortedNumbers = numbers.sort((a, b) => a - b)

console.log(sortedNumbers) // Output: [1, 2, 3, 4, 5]

_Example 2_
const words = ['zebra', 'dog', 'cat', 'bear', 'elephant']
// Use .sort() to sort the words in alphabetical order
const sortedWords = words.sort()

console.log(sortedWords) // Output: ['bear', 'cat', 'dog', 'elephant', 'zebra']
Enter fullscreen mode Exit fullscreen mode

5 .forEach()

The .forEach() method executes a provided function once for each array element. Unlike .map() and .filter(), .forEach() does not return a new array. It's a great way to perform an action on each element in an array without transforming the data.

Here's an example:

const numbers = [1, 2, 3, 4, 5]
// Use .forEach() to log each number
numbers.forEach(num => console.log(num))

// Output:
// 1
// 2
// 3
// 4
// 5

_Example 2_
const words = ['zebra', 'dog', 'cat', 'bear', 'elephant']

words.forEach((word, index) => {
  words[index] = word.charAt(0).toUpperCase() + word.slice(1)
})

console.log(words) // Output: ['Zebra', 'Dog', 'Cat', 'Bear', 'Elephant']
Enter fullscreen mode Exit fullscreen mode

Conclusions

In conclusion, these are the top 5 JavaScript array methods that you should master. Whether you’re transforming data, extracting a subset, reducing to a single value, sorting, or performing an action, there’s a method to fit the task.

With these tools in your toolbox, you’ll be able to tackle any array-related problem with ease.

Happy coding! ✌️

Top comments (0)