DEV Community

Cover image for 9 more Javascript Array methods
zuzexx
zuzexx

Posted on

9 more Javascript Array methods

Array methods are a powerful tool in JavaScript that allow developers to manipulate and transform arrays in various ways. These methods are built-in functions that can be used on any array object, making them an essential part of the JavaScript language. We already explored some array methods in this article, today we will explore 9 more methods.

Table of Contents

  1. .find()
  2. .findIndex()
  3. .includes()
  4. .forEach()
  5. .map()
  6. .sort()
  7. .reverse()
  8. .filter()
  9. .reduce()

.find()

The Array.find() method returns the value of the first element in the array that satisfies the provided testing function. It returns undefined if no elements in the array pass the test.

let numbers = [1, 2, 3, 4, 5];
let result = numbers.find(num => num > 3);
console.log(result); // 4

Enter fullscreen mode Exit fullscreen mode

.findIndex()

The Array.findIndex() method returns the index of the first element in the array that satisfies the provided testing function. It returns -1 if no elements in the array pass the
test.

let numbers = [1, 2, 3, 4, 5];
let result = numbers.findIndex(num => num > 3);
console.log(result); // 3
Enter fullscreen mode Exit fullscreen mode

.includes()

The Array.includes() method returns a boolean indicating whether the given value is present in the array.

let numbers = [1, 2, 3, 4, 5];
let result = numbers.includes(3);
console.log(result); // true
Enter fullscreen mode Exit fullscreen mode

.forEach()

The Array.forEach() method executes a provided function once for each array element.

let numbers = [1, 2, 3, 4, 5];
numbers.forEach(num => console.log(num));
Enter fullscreen mode Exit fullscreen mode

.map()

The Array.map() method creates a new array with the results of calling a provided function on every element in the calling array.

let numbers = [1, 2, 3, 4, 5];
let result = numbers.map(num => num * 2);
console.log(result); // [2, 4, 6, 8, 10]
Enter fullscreen mode Exit fullscreen mode

.sort()

The Array.sort() method sorts the elements of an array in place and returns the sorted array.

let numbers = [5, 3, 1, 4, 2];
numbers.sort();
console.log(numbers); // [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

.reverse()

The Array.reverse() method reverses the order of the elements of an array in place and returns the reversed array.

let numbers = [1, 2, 3, 4, 5];
numbers.reverse();
console.log(numbers); // [5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

.filter()

The Array.filter() method creates a new array with all elements that pass the test implemented by the provided function.

let numbers = [1, 2, 3, 4, 5];
let result = numbers.filter(num => num > 3);
console.log(result); // [4, 5]
Enter fullscreen mode Exit fullscreen mode

.reduce()

The Array.reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

let numbers = [1, 2, 3, 4, 5];
let result = numbers.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(result); // 15
Enter fullscreen mode Exit fullscreen mode

Conclusion:

In conclusion, these array methods are essential for working with arrays in JavaScript. They allow developers to manipulate, sort, filter, and transform arrays in various ways. Whether you're working on a simple or complex project, understanding how to use these methods will make your work more efficient and effective. I hope this article has given you a good overview of the most commonly used array methods in JavaScript, and how they can be applied in real-world scenarios.

Top comments (0)