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

Returns the unique symmetric difference between two arrays, not containing duplicate values from either array

Example:

const uniqueSymmetricDifference = (a, b) => [
  ...new Set([...a.filter(v => !b.includes(v)), ...b.filter(v => !a.includes(v))])
];
console.log(uniqueSymmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
console.log(uniqueSymmetricDifference([1, 2, 2], [1, 3, 1])); // [2, 3]

Output:

[3, 4]
[2, 3]