Open In App

Remove the last Item From an Array in JavaScript

To Remove the last item from an array in JavaScript, we have multiple approaches.

Methods to Remove the Last Element from an Array:



Method 1: Using Array splice() Method

This method uses Array.splice() method that adds/deletes items to/from the array and returns the deleted item(s).



Syntax:

array.splice(index, number, item1, ....., itemN)

Parameters:

Return value:

Returns a new Array, having the removed items.

Example: This example removes the last item from the array using the splice() method.




// Input array
let array = [34, 24, 31, 48];
 
// Display array
console.log("Array = [" + array + "]");
 
// Funciton to remove last element
function gfg_Run() {
    array.splice(-1, 1);
     
    // Display Output
    console.log(
        "Remaining array = [" + array + "]");
}
gfg_Run()

Output
Array = [34,24,31,48]
Remaining array = [34,24,31]

Method 2: Using Array slice() Method

In this appraoch we are using Array.slice() method. This method returns a new array containing the selected elements. This method selects the elements that start from the given start argument and end at, but excludes the given end argument. 

Syntax:

array.slice(start, end)

Parameters:

Return value:

Returns a new Array, having the selected items.

Example: This example does not remove the last item from the array but returns a new array in which the item is removed, using the slice() method.




// Input array
let array = [34, 24, 31, 48];
// Display input array
console.log("Array = [" + array + "]");
 
// Function to remove last element
function gfg_Run() {
    // Display output
    console.log(
        "Remaining array = [" + array.slice(0, -1) + "]");
}
gfg_Run()

Output
Array = [34,24,31,48]
Remaining array = [34,24,31]

Method 3: Using Array pop() Method

In this appraoch, we are using Array.pop() method. This method deletes the last element of an array and returns the element. 

Syntax:

array.pop()

Return value:

It returns the removed array item. An array item can be a string, a number, an array, a boolean, or any other object type that is applicable to an array.

Example: This example removes the last item from the array using the pop() method.




// Input array
let array = [34, 24, 31, 48];
// Display input array
console.log("Array = [" + array + "]")
 
//Funcion to remove element and show output
function gfg_Run() {
    // Remove last element
    array.pop();
     
    // Display output
    console.log(
        "Remaining array = [" + array + "]");
}
gfg_Run()

Output
Array = [34,24,31,48]
Remaining array = [34,24,31]

Method 4: Using array.reduce() Method

The array.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array and the return value of the function is stored in an accumulator.

Syntax:

array.reduce( function(total, currentValue, currentIndex, arr), initialValue )

Example: This example removes the last item from the array using the arr.reduce() method.




// Input array
let array = [34, 24, 31, 48];
// Display input array
console.log("Array = [" + array + "]");
 
// Function to remove last element
function gfg_Run() {
    array = array.filter((item, i) => i != (array.length - 1));
     
    // Display output
    console.log(
        "Remaining array = [" + array + "]");
}
gfg_Run()

Output
Array = [34,24,31,48]
Remaining array = [34,24,31]

Method 5: Using Array.length

The array length property in JavaScript is used to set or return the number of elements in an array. 

Syntax:

array.length

Example: myArray.length returns the current length of the array. By setting myArray.length = myArray.length - 1, you effectively remove the last item from the array.




let myArray = [1, 2, 3, 4, 5];
 
// Use the length property to remove the last item
myArray.length = myArray.length - 1;
 
console.log(myArray); // Output: [1, 2, 3, 4]

Output
[ 1, 2, 3, 4 ]

Article Tags :