How to remove last array element in JavaScript and return it?

JavascriptWeb DevelopmentFront End Technology

To remove last array element in JavaScript, use the pop() method. JavaScript array pop() method removes the last element from an array and returns that element.

Example

You can try to run the following code to remove last array element −

Live Demo

<html>
   <head>
      <title>JavaScript Array pop Method</title>
   </head>
   <body>
      <script>
         var numbers = [40, 70, 80, 100];
         var element = numbers.pop();
         document.write("element is : " + element );
         var element = numbers.pop();
         document.write("<br />element is : " + element );
      </script>
   </body>
</html>

Output

element is : 100
element is : 80
raja
Published on 12-Feb-2018 12:42:34
Advertisements