Published on 06/19/2023
In JavaScript, arrays are one of the most commonly used data structures. They are a collection of elements, each with a unique index or key. JavaScript offers a variety of methods to work with arrays that make it easy to manipulate and transform your data.
This short series will list out some of the most commonly used array methods, grouped into three classifications (destructive, non-destructive, and static). We will also see a short example for each method to illustrate how it works.
Part 1: Destructive Methods (this post)
Part 2: Non-Destructive Methods
Part 3: Static Methods (coming soon)
Let's jump right in! In this first part of the series, we will cover some of the destructive Array methods in JavaScript. These methods, also known as mutative methods, modify the original array they are called on, thus making them destructive. They are useful when you specifically want to update or alter the existing data within an array.
The push() method is used to add elements to the end of an array. It takes one or more arguments, which represent the elements to be added.
The pop() method is used to remove the last element from an array. It does not take any arguments.
The shift() method is used to remove the first element from an array. It does not take any arguments.
The unshift() method is used to add elements to the beginning of an array. It takes one or more arguments, which represent the elements to be added to the array.
The splice() method modifies an array by removing or replacing existing elements and/or adding new elements in place. It returns an array containing the removed elements, or an empty array if no elements were removed.
It takes two main arguments, which represent the starting and ending positions of the section to be extracted – the first being the zero-based index to be used for the starting point and the second (optional) being the delete count, i.e., the number of elements in the array to remove from the starting point.
If the second parameter is omitted, or if its value is greater than or equal to the number of elements after the position specified by the first parameter, then all the elements from the starting position to the end of the array will be deleted.
Any additional arguments that are optionally passed after the delete count will represent items that will be added to the array, beginning from the argument passed in as the starting point.
The reverse() method is used to reverse the order of the elements in an array. It doesn't take any arguments.
The sort() method sorts the elements of an array in place (no copy is made) and returns the reference to the original array, in sorted order. It takes one argument, which is a function that represents the order in which the elements should be sorted.
The function is called two arguments, which represent the first and second elements in position for comparison.
This will return a negative value if a should come before b, and vise versa. A zero value will return if a and b are equal.
By default, if no function is passed, the elements in the array will be converted to strings and ordered based on their Unicode code points.
To sort the elements in an array without mutating the original array, use toSorted().
The fill() method is used to fill all elements in an array with a specified value. It does this by converting all of the elements in the array to a static value, from a start index (default 0) to an end index (default array.length), and returns the modified array.
This method takes three parameters:
the value to fill the array with.
A zero-based start index (optional).
A zero-based end index (optional).
In this part, you’ve learned about some of the destructive methods in JavaScript, along with short examples on how to use them.
Next up in this series, we’re going to cover JavaScript's non-destructive methods.
Part 1: Destructive Methods (you are here)
Part 2: Non-Destructive Methods
Part 3: Static Methods (coming soon)
---
Category: JavaScript
Tags: core-js, javascript-arrays