Home > Web Front-end > JS Tutorial > body text

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

黄舟
Release: 2017-11-03 11:17:31
Original
3127 people have browsed it

Definition and Usage

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

Syntax

arrayObject.pop()
Copy after login

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>
Copy after login

Output:

George,John,Thomas
Thomas
George,John
Copy after login

Here are the details of the parameters:

NA
Copy after login

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>
Copy after login

This will produce the following results:

element is : 9
element is : 4
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!