pop() - JavaScript Tutorial

pop()

BLUF: This tutorial on pop() provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: pop()

Understanding pop() is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience