Get the App
SLTechnology News&Howtos  ›  Development  › 

How to delete the last element in the javascript array

Shulou Source: shulou.com Published: 2022-06-02 11:24:23 09月26日 Update

This article mainly explains "how to delete the last element of the javascript array". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to delete the last element of the javascript array".

Javascript array delete the last element of the method: 1, the use of the length attribute, the value of the attribute will be set less than the original array length of 1, syntax "arr.length= the original array length-1"; 2, the use of array pop () method, syntax "arr.pop ()".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The javascript array deletes the last element

Method 1: use the length attribute-to delete one or more elements at the end of the array

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

We can delete one or more elements from the end of the array by setting the length property smaller than the original length by setting the length of the array.

Var a = [1, var 2, 3, 5, 5, 6, 7, and 8]; / / define an array, a.memthology, 5, console.log (a), var a = [1, 2, 3, 5, 5, 6, 6, 5, 7, 8]; / / define an array, a. Paraphrase, 6, console.log (a) var a = [1, 2, 4, 5, 5, 6, 7, 8]; / / define an array, a. Paraphrase, 3, console.log (a)

If the value is set to 0, all arrays will be deleted, that is, empty arrays!

Var a = [1, 2, 3, 4, 5, 6, 7, 8]; / / defines the array a.

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

The array.pop () method deletes the last element in the array array and returns the deleted element.

Let's take a look at the following examples:

Var a = [1, console.log 2, 3, 4, 5, 6, 7, 8]; / / define the array a.pop (); console.log (a)

Thank you for your reading, the above is the content of "how to delete the last element of the javascript array". After the study of this article, I believe you have a deeper understanding of the problem of how to delete the last element of the javascript array. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Arrays elements methods attributes length learning content multiple endings grammars examples that is tails ideas situations tutorials numbers articles more features Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MySQL Xiaomi Apple NVidia