Skip to content
the poor coder

TOP Array Methods

Poor Man
Published
Reading time
2 min read
On this page

TOP Array Methods#

Arrays are an important part of JavaScript programming language. They are mutable, which means that their elements can be added, removed, or replaced. In this article, we will discuss the top Array methods in JavaScript.

forEach()#

The forEach() method executes a provided function once for each array element. This method does not return a new array, but it can modify the existing array. The syntax for this method is:

JavaScript

array.forEach(function(element, index, array) {
  // Do something with element
});

The function takes three parameters:

  • element: The current element being processed in the array.
  • index: The index of the current element being processed in the array.
  • array: The array that forEach() is being applied to.

filter()#

The filter() method creates a new array with all elements that pass the test implemented by the provided function. The syntax for this method is:

JavaScript

var newArray = array.filter(function(element) {
  return element > 10;
});

The function takes one parameter:

  • element: The current element being processed in the array.

The filter() method creates a new array with all elements that pass the test implemented by the provided function. If no elements pass the test, an empty array will be returned.

map()#

The map() method creates a new array with the results of calling a provided function on every element in the calling array. The syntax for this method is:

JavaScript

var newArray = array.map(function(element) {
  return element * 2;
});

The function takes one parameter:

  • element: The current element being processed in the array.

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

reduce()#

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

JavaScript

var newArray = array.reduce(function(accumulator, currentValue) {
  return accumulator + currentValue;
});

The function takes two parameters:

  • accumulator: The accumulated value previously returned in the last invocation of the callback, or initialValue.
  • currentValue: The current element being processed in the array.

The reduce() method reduces the array to a single value. This value can be a number, string, object, or anything else, based on what you return from the callback function.

sort()#

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. The syntax for this method is:

JavaScript

array.sort(function(a, b) {
  return a - b;
});

The function takes two parameters:

  • a: The first element for comparison.
  • b: The second element for comparison.

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, but you can provide your own comparison function to specify a custom sort order.

Conclusion#

These are the top Array methods in JavaScript. Each method has its own unique purpose, and knowing when to use each method will make your code more efficient and easier to read.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC