In JavaScript, an array is a type of data structure that is used to store a collection of values. Often, it is necessary to remove the last element of an array, whether it is no longer needed or because the array has been processed to the desired length.
There are a few ways to remove the last element of an array in JavaScript, and in this article, we’ll explore a few of them.
Method 1: Using the pop()
method:
The pop()
method is a built-in function in JavaScript that removes the last element of an array and returns it.
Here is an example:
let myArray = [1, 2, 3, 4, 5];
let lastElement = myArray.pop();
console.log(myArray); // Output: [1, 2, 3, 4]
console.log(lastElement); // Output: 5
In the above example, we declare an array myArray
containing the values [1, 2, 3, 4, 5]
. We then call the pop()
method on the array, which removes the last element (5
) and returns it. We store the returned value in a variable called lastElement
. Finally, we log both the modified myArray
and the removed lastElement
to the console.
Method 2: Using the splice()
method:
The splice()
method is another built-in function in JavaScript that can be used to remove elements from an array. In addition to removing elements, the splice()
method can also be used to insert or replace elements.
Here’s an example of how to use it to remove the last element of an array:
let myArray = [1, 2, 3, 4, 5];
myArray.splice(-1, 1);
console.log(myArray); // Output: [1, 2, 3, 4]
In this example, we declare an array myArray
containing the values [1, 2, 3, 4, 5]
. We then call the splice()
method on the array, passing in two arguments: the index
of the element to start the modification at (in this case, -1
, which is equivalent to the last element), and the number of elements to remove (in this case, 1
). The splice()
method modifies the array in place and returns an array containing the removed elements (in this case, [5]
). Since we don’t need to store the removed elements, we don’t assign the return value of splice()
to a variable. Finally, we log the modified myArray
to the console.
Method 3: Using the slice()
method:
The slice()
method is a built-in function in JavaScript that can be used to extract a portion of an array into a new array. We can use the slice()
method to create a new array that contains all elements except for the last one.
Here’s an example of how to use it:
let myArray = [1, 2, 3, 4, 5];
let newArray = myArray.slice(0, -1);
console.log(myArray); // Output: [1, 2, 3, 4, 5]
console.log(newArray); // Output: [1, 2, 3, 4]
In this example, we declare an array myArray
containing the values [1, 2, 3, 4, 5]
. We then call the slice()
method on the array, passing in two arguments: the index
of the first element to include in the new array (in this case, 0
), and the index of the last element to exclude (in this case, -1
, which is equivalent to the last element). The slice()
method returns a new array containing the selected elements, which we store in a variable called newArray
. Finally, we log both the original myArray
and the new newArray
to the console.
Method 4: Using the length property:
The length
property of an array in JavaScript represents the number of elements in the array. We can use the length
property to remove the last element of an array by simply setting the length of the array to be one less than its current length.
Here’s an example of how to use this technique:
let myArray = [1, 2, 3, 4, 5];
myArray.length = myArray.length - 1;
console.log(myArray); // Output: [1, 2, 3, 4]
In this example, we declare an array myArray
containing the values [1, 2, 3, 4, 5]
. We then set the length property of myArray
to be one less than its current length (i.e., 4
). This effectively removes the last element of the array. Finally, we log the modified myArray
to the console.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.