JavaScript pop() | Remove Last Element from an Array

The JavaScript pop() method is used when we need to remove the last element from an array. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p>Array before pop():-<br><span id="xyz"></span></p>
   <hr>
   <p>Array after pop():-<br><span id="abc"></span></p>

   <script>
      const cities = ["Tokyo", "Bangkok", "Dubai", "Berlin", "Frankfurt"];

      document.getElementById("xyz").innerHTML = cities;
      cities.pop();
      document.getElementById("abc").innerHTML = cities;
   </script>
   
</body>
</html>
Output

Array before pop():-


Array after pop():-

JavaScript pop() Syntax

The syntax of pop() method in JavaScript is:

array.pop()

The pop() method returns the removed element from the array. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="res"></p>

   <script>
      const x = ["Tokyo", "Bangkok", "Dubai", "Berlin", "Frankfurt"];
      document.getElementById("res").innerHTML = x.pop();
   </script>
   
</body>
</html>
Output

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋