Home  >  Article  >  Web Front-end  >  JavaScript method pop() to delete and return the last element of an array

JavaScript method pop() to delete and return the last element of an array

黄舟
黄舟Original
2017-11-03 11:17:313128browse

Definition and Usage

pop() method is used to remove and return the last element of the array.

Syntax

arrayObject.pop()

Return value

The last element of arrayObject.

Description

The pop() method will delete the last element of arrayObject, reduce the array length by 1, and return the value of the element it deletes. If the array is already empty, pop() does not modify the array and returns an undefined value.

Example

In this example, we will create an array and then delete the last element of the array. Note that this will also change the length of the array:

<script type="text/javascript">

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"document.write(arr)document.write("<br />")document.write(arr.pop())document.write("<br />")document.write(arr)</script>

Output:

George,John,Thomas
Thomas
George,John

Here are the details of the parameters:

NA

Return value:

Returns the element removed from the array.

Example:

<html>
<head>
<title>JavaScript Array pop Method</title>
</head>
<body>
<script type="text/javascript">
var numbers = [1, 4, 9];
 
var element = numbers.pop();
document.write("element is : " + element ); 
 
var element = numbers.pop();
document.write("<br />element is : " + element ); 
</script>
</body>
</html>

This will produce the following results:

element is : 9
element is : 4


The above is the detailed content of JavaScript method pop() to delete and return the last element of an array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn