The pop method in JavaScript is utilized to eliminate the final element from an array and subsequently returns that element. This operation modifies the length of the original array.
Syntax
The syntax for the pop method is as follows:
Example
array.pop()
Return
The last element of given array.
JavaScript Array pop method example
Let's see some examples of pop method.
Example 1
In this section, we will remove an element from the specified array.
Example
<script>
var arr=["AngularJS","Node.js","JQuery"];
document.writeln("Orginal array: "+arr+"<br>");
document.writeln("Extracted element: "+arr.pop()+"<br>");
document.writeln("Remaining elements: "+ arr);
</script>
Output:
Output
Orginal array: AngularJS,Node.js,JQuery
Extracted element: JQuery
Remaining elements: AngulaJS,Node.js
Example 2
In this demonstration, we will remove all elements from the specified array using the pop operation.
Example
<script>
var arr=["AngulaJS","Node.js","JQuery"];
var len=arr.length;
for(var x=1;x<=len;x++)
{
document.writeln("Extracted element: "+arr.pop()+"<br>");
}
</script>
Output:
Output
Extracted element: JQuery
Extracted element: Node.js
Extracted element: AngulaJS