Use pop() method to remove the last item in the array in JavaScript

Description

The following code shows how to use pop() method to remove the last item in the array.

Example


<!--   ww  w.j  ava 2s  .c  o m-->

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var colors = new Array(); //create an array
var count = colors.push("A", "B"); //push two items
document.writeln(colors);
document.writeln("<br/>");

var item = colors.pop(); //get the last item
document.writeln(item);
document.writeln("<br/>");
document.writeln(colors.length);
document.writeln("<br/>");
document.writeln(colors);

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Use pop() method to remove the last item in the array in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      Array
...
Loop through an array with for statement in...
Loop through array and work on each element...
Map array value in JavaScript
Move data from an Array to another in JavaS...
Output array with toString() in JavaScript
Output array with valueOf() in JavaScript
Push form value to a stack in JavaScript
Push two values to an empty array in JavaSc...
Reduce an array from the end with reduceRig...
Reduce array to a value in JavaScript
Reference array element by random index val...
Remove elements from array by changing the ...
Remove the first item in the array and retu...
Replace array element with Array splice() i...
Reverse an array in JavaScript
Search an array for an object with indexOf ...
Search an array from back at a specific end...
Search an array from start with indexOf met...
Search an array from starting index with in...
Search an array from the end for an object ...
Search an array from the end with lastIndex...
Set array element value with index in JavaS...
Slice an array to get a sub-array in JavaSc...
Slice array with negative value in JavaScri...
Sort Array Based on Argument Lengths in Jav...
Sort in descending order with custom method...
Use Array as a stack in JavaScript
Use Array's Constructor to create arrays in...
Use array forEach() method to run a functio...
Use array.valueOf with a two dimensional ar...
Use both push and index to add data to an a...
Use default Sort method on an array in Java...
Use for each loop to change the array eleme...
Use for loop to initialize an array in Java...
Use function to populate array with string ...
Use pop() method to remove the last item in...
Use string as the array index in JavaScript
Use the sort() method on numbers and string...
...