w3resource

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 example

See 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.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

instance

class Calc {
	constructor() {
		this.count = 0 
	}

	increase() {
		this.count ++
	}
}

const calc = new Calc()
new Calc().increase()

console.log(calc.count)

We set the variable calc equal to a new instance of the Calc class. Then, we instantiate a new instance of Calc, and invoke the increase method on this instance. Since the count property is within the constructor of the Calc class, the count property is not shared on the prototype of Calc. This means that the value of count has not been updated for the instance calc points to, count is still 0.

Ref: https://bit.ly/2Hcpkm6