TOP Array Methods

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:


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:


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:


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:


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:


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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe