Home > Web Front-end > JS Tutorial > How to delete the last element of a javascript array

How to delete the last element of a javascript array

青灯夜游
Release: 2021-11-24 14:27:12
Original
52862 people have browsed it

How to delete the last element of a javascript array: 1. Use the length attribute to set the value of the attribute to 1 less than the original array length. The syntax is "arr.length=original array length-1"; 2. Use the pop() method of the array with the syntax "arr.pop()".

How to delete the last element of a javascript array

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript array deletes the last element

Method 1: Use the length attribute----you can delete an or at the end of the array Multiple elements

The length attribute of an array is generally used to set or return the number of elements in the array, that is, to set or return the array length.

We can use the feature of setting the length of the array to set the length attribute smaller than the original length to delete one or more elements from the end of the array;

var a = [1,2,3,4,5,6,7,8];  //定义数组
a.length=7;
console.log(a)

var a = [1,2,3,4,5,6,7,8];  //定义数组
a.length=6;
console.log(a)

var a = [1,2,3,4,5,6,7,8];  //定义数组
a.length=3;
console.log(a)
Copy after login

How to delete the last element of a javascript array

If the value is set to 0, the entire array will be deleted, that is, the array will be cleared!

var a = [1,2,3,4,5,6,7,8];  //定义数组
a.length=0;
console.log(a)
Copy after login

How to delete the last element of a javascript array

Method 2: Use the pop() method----you can delete an element at the end of the array

array.pop () method can delete the last element in the array and return the deleted element.

Let’s take a closer look at the following example:

var a = [1,2,3,4,5,6,7,8];  //定义数组
a.pop();
console.log(a)
Copy after login

How to delete the last element of a javascript array

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to delete the last element of a javascript 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