JavaScript – Remove Last Element of Array
To remove last element of an array in JavaScript, call pop() method on this array. pop() method modifies the original array, and returns the removed element.
</>
Copy
arr.pop()
Example
In the following example, we have taken an array in arr
. We remove the last element from this array using pop() method.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<pre id="output"></pre>
<script>
var arr = ['apple', 'banana', 'cherry'];
displayOutput = 'Input Array : ' + arr + '\n';
var removedItem = arr.pop();
displayOutput += 'Removed Item : ' + removedItem + '\n';
displayOutput += 'Result Array : ' + arr;
document.getElementById("output").innerHTML = displayOutput;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to remove last element of an array using pop() method.