Use the JavaScript Array pop() method to remove the last element of an array. This method removes the last element of an array and returns the element it removes.
array.pop()
Note: Pop() method will change the length of the array in JS.
Remove the last item from an array in JavaScript
HTML example code.
When you execute pop()
function even though the line returns the popped item the original array is affected and the popped element is removed.
<!DOCTYPE html>
<html>
<body>
<script>
var arr = [1,2,3,4];
var ele = console.log(arr.pop());
console.log(arr);
console.log(ele);
</script>
</body>
</html>
Output:
You can do it in two ways using splice()
:
arr.splice(-1,1)
arr.splice(arr.length-1,1)
Do comment if you have any doubts or suggestions on this JS Array code.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version