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

Profile image for GGDev
GGDev Software Engineer
Feb 11, 2023 ‧ 2 min read
Series (2 Parts): Javascript Array Methods

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.

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:

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]


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!']

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:

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]


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']

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:

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



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'

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 ifa should be sorted before b
- 0 if a and bare equal
- a positive value if a should be sorted after b

Here's an example:

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]


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']

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

const words = ['dog', 'cat', 'elephant', 'bear', 'zebra'];

// Use .forEach() to log each word
words.forEach(word => console.log(word));

// Output:
// dog
// cat
// elephant
// bear
// zebra

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! ✌️

Posted on Feb 11, 2023 by:
Profile image for GGDev
GGDev
Software Engineer
JavaScript TypeScript Node.js React MySQL GraphQL

Comments

Profile image for GGDev

Software Engineer

JavaScript TypeScript Node.js React MySQL GraphQL
15
Reputation
0
Following
0
Followers