How To Remove Array Last Element In JavaScript With Example

admin_img Posted By Bajarangi soft , Posted On 25-09-2020

JavaScript is a dynamic programming language. Java Script can combine with any other language ( Its used as a part of web pages), whose implementations allow client-side script to interact with the user and make dynamic pages. It is an interpreted programming language with object-oriented capabilities. In The JavaScript we have so many in built method so today we are going to discuss how to remove last element from an array in java script

How To Remove Array Last Element In JavaScript With Example

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"];


2.Now we implement code to remove array last element
 
document.getElementById("demo").innerHTML = flowers;

function remove_last_element() {
    flowers.pop();
    document.getElementById("demo").innerHTML = flowers;
}


Complete Code for removeing last array element in java script
<!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>

Related Post