Array pop() method with example in JavaScript

JavaScript pop() method: Here, we are going to learn about the pop() method of array in JavaScript.
Submitted by IncludeHelp, on February 27, 2019

JavaScript pop() method

pop() method is used remove an element from the end of an array, it changes the length of the array.

Syntax:

    array_name.pop();

Examples:

    Input:
    var countries = ["INDIA", "USA", "UK", "CANADA", "RUSSIA"];

    Function call:
    countries.pop();
    countries.pop();
    countries.pop(); 
    
    Output:
    "INDIA", "USA"

JavaScript Code to demonstrate example of Array.pop() method

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		var countries = ["INDIA", "USA", "UK", "CANADA", "RUSSIA"];
		document.write("countries: " + countries + "<br>");
		document.write("length is: " + countries.length + "<br>");
		
		//removing elements
		countries.pop();
		countries.pop();
		countries.pop();

		document.write("countries: " + countries + "<br>");
		document.write("length is: " + countries.length + "<br>");		
	</script>
</body>
</html>

Output

countries: INDIA,USA,UK,CANADA,RUSSIA
length is: 5
countries: INDIA,USA
length is: 2

JavaScript Array Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.