Sitemap
Stefan

Stefan

Freelance software engineer and teacher

JavaScript Array Methods

2 min readJul 24, 2024

When you begin developing with JavaScript, you will undoubtedly encounter array methods.

These methods are extremely helpful because they allow you to write more efficient, organized, and readable code.

Get Stefan’s stories in your inbox

Join Medium for free to get updates from this writer.

I like to classify them into two categories:

  • Methods that modify the original array
  • Methods that don’t modify the original array

Methods that modify the original array are:

  • push(...elementsToAdd) : Adds one or more elements to the end of the array and returns the new length.
  • pop(): Removes the last element from the array and returns the removed element.
  • shift() : Removes the first element from the array and returns the removed element.
  • unshift(...elementsToAdd) : Adds one or more elements to the beginning of the array and returns the new length.
  • splice(startIndex, nToRemove, ...elementsToAdd) : Adds or removes elements from an array and returns an array with the removed elements. The first parameter specifies the position at which to start adding or removing elements. The second parameter specifies the number of elements to remove. The remaining parameters specify the elements to be added.
  • sort([comparisonFunction]) : Sorts an array alphabetically in ascending order. By default, this method works with arrays of strings. To handle other data types, you can pass a comparison function as a parameter. When the sort() method compares two values, it sends them to the comparison function and sorts them based on the returned value, which can be positive, negative, or zero.
  • reverse() : Reverses the order of the elements in an array.

Methods that don’t modify the original array are:

  • filter(testFunction) : Returns a new array consisting of the elements from the original array that pass the test provided by a callback function.
  • map(transformFunction) : Returns a new array consisting of the elements from the original array, each transformed by a callback function passed as a parameter.
  • reduce(reducerFunction) : Executes a reducer function on every element of the array and returns the final accumulated value.
  • includes(element) : Returns true if the array contains the specified element; otherwise, it returns false.
  • indexOf(element) : Returns the first index of the specified element. If the element is not found, it returns -1.
  • lastIndexOf(element) : Returns the last index of the specified element. If the element is not found, it returns -1.
  • join([separator]) : Returns a string created by concatenating the elements of the array. The default separator is a comma, but a different separator can be specified by passing a parameter.
  • slice([startIndex], [endIndex]) : Returns a new array containing elements from the original array, starting at startIndex (inclusive) and ending at endIndex (exclusive).
  • concat(...arrays) : Returns a new array created by concatenating the elements of the calling array with the elements of the arrays passed as parameters.
  • flat([depth]) : Returns a new array created by concatenating all the elements of nested arrays. The depth parameter specifies how deep the nesting should be flattened.

So, these are some of the most commonly used JavaScript array methods. Trust me, instead of always resorting to for loops, consider whether you can solve the problem using one or a combination of these methods.

I hope you found this article useful and insightful.

Until next time!

--

--

Stefan
Stefan

Written by Stefan

Freelance software engineer and teacher