JavaScript pop() Method: Array Object
Description
The pop() method is used to remove the last element from an array. This method changes the length of the array i.e. one less than it's current value.
Version
Implemented in JavaScript 1.2
Syntax
pop()
Parameters
None
Example:
In the following web document pop() method is used to remove the last element of the given array.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>JavaScript pop() method example</title>
<style type="text/css">
h1 {color:red}
</style>
</head>
<body>
<h1>JavaScript : pop() method</h1>
<script src="array-pop-example1.js">
</script>
</body>
</html>
JS Code
var fruitslist = new Array("Orange", "Apple", "Banana", "Chery");
var newParagraph = document.createElement("p");
var newText = document.createTextNode("Fruits List : " + fruitslist);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var fruitspop=fruitslist.pop();
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("Removed element is : " + fruitspop);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
var newParagraph2 = document.createElement("p");
var newText2 = document.createTextNode("The current length of the array is : "+fruitslist.length);
newParagraph2.appendChild(newText2);
document.body.appendChild(newParagraph2);
View the example in the browser
Practice the example above online
JavaScript pop() method exampleSee also:
JavaScript Core objects, methods, properties.
Previous: JavaScript join() Method: Array Object
Next:
JavaScript push() Method: Array Object
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/javascript/object-property-method/array-pop.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics