2 Example(s) of JavaScript pop function


Description :

JavaScript pop is use to remove the last element from the array. See the code snippet:


JavaScript pop function Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="removeElement()">Remove the last element</button>
<p id="sample"></p>
<script>
var fishName= ['angel', 'clown', 'mandarin', 'sturgeon'];
document.getElementById("sample").innerHTML = fishName;

function removeElement() {
    fishName.pop();
    document.getElementById("sample").innerHTML = fishName;
}
</script>

</body>
</html>

Output

Description :

Example of JavaScript Pop function:


JavaScript pop function Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
 <script>
        var numbers = [1, 4, 9];
        var element = numbers.pop();
        document.write("element is : " + element ); 
        var element = numbers.pop();
        document.write("<br />element is : " + element );
  </script>
</body>
</html>

Output