JavaScript Array pop()

Posted by

Method

JavaScript Array pop() Method

The JavaScript Array.pop() method is used to remove the last element from an array and return that element. It changes the length of the array. The pop() method removes the last element from an array and returns that element. This method modifies the array on which it is called.

Syntax

The syntax of the Array.pop() method is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

array.pop()

[/dm_code_snippet]

Parameters

The Array.pop() method takes no parameters.

Return Value

The Array.pop() method returns the removed element from the array.

Example

The following example shows the use of the Array.pop() method:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// Create an array
var fruits = ["Banana", "Orange", "Apple", "Mango"];

// Get the last element of the array
var lastElement = fruits.pop();

// Print the last element
console.log(lastElement); // Output: Mango

// Print the array
console.log(fruits); // Output: ["Banana", "Orange", "Apple"]

[/dm_code_snippet]

In the above example, the pop() method is used to remove the last element (Mango) from the fruits array and returns it. The returned element is then stored in the lastElement variable. The fruits array is then printed to the console, which now has a length of 3.

Browser Support

The Array.pop() method is supported in all modern browsers, including Internet Explorer 11 and above.