The pop() method removes the last element of an array, and returns that element.
Note: This method changes the length of an array.
Syntax and Usage
array.pop()
Let's Start
1.Create Array
var flowers = ["Lily", "Rose", "Lotus", "Jasmine"];
document.getElementById("demo").innerHTML = flowers;
function remove_last_element() {
flowers.pop();
document.getElementById("demo").innerHTML = flowers;
}
<!DOCTYPE html>
<html>
<head>
<title>Remove Array Last Element In JavaScript</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<br>
<div class="text-center">
<h1>Remove Array Last Element In JavaScript</h1>
</div>
<br>
<div class="well">
<button class="btn btn-primary" onclick="remove_last_element()">Click it</button>
<h1 id="demo"></h1>
</div>
<br>
</div>
</body>
</html>
<script>
var flowers = ["Lily", "Rose", "Lotus", "Jasmine"];
document.getElementById("demo").innerHTML = flowers;
function remove_last_element() {
flowers.pop();
document.getElementById("demo").innerHTML = flowers;
}
</script>