How to use Array pop() Method In Javascript
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]
Remove the last Item From an Array in JavaScript
Removing the last item from an array in JavaScript is a fundamental operation often encountered in array manipulation tasks. It involves eliminating the last element of an array, which can be useful for dynamically adjusting array contents based on various conditions or requirements within a program.
Methods to Remove the Last Element from an Array:
Table of Content
- Using Array splice() Method
- Using Array slice() Method
- Using Array pop() Method
- Using array.reduce() Method
- Using Array.length
- Using Array.filter() Method