Get the App
SLTechnology News&Howtos  ›  Development  › 

Whether the array length can be changed in JavaScript

Shulou Source: shulou.com Published: 2022-06-02 18:26:27 10月01日 Update

This article mainly introduces whether the length of the array in JavaScript can be changed, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

JavaScript array length can be changed, methods: 1, by changing the value of the array length attribute to change the array length, syntax "arr.length= length value"; 2, the use of unshift (), shift (), splice () and other methods to add or delete array elements to change the array length.

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

The array length in JavaScript can be changed.

The length attribute of the JavaScript array is variable, and we can change the length of the array by changing the value of the length attribute.

Var arr=; / / an array containing 8 values arr [xss_clean] (arr.length); / / displays the array length 8arr.array 10; / / increases the length of the array [xss_clean] (arr.length); / / the array length has changed to 10.

Also, the length of the array changes as the element increases or decreases. Example:

Var arr= [98 arr.length 76]; / / Array containing 5 values [xss_clean] (arr.length); / / display array length 5arr [15] = 34; / / add elements, use index 15, assign to 34alert (arr.length); / / display array length 16

We can use push (), pop (), unshift (), shift (), splice () and other methods to add or remove array elements to change the array length. Example:

Var arr = [0]; / / define the array console.log (arr.length); arr.unshift (1Power2); / / add two elements at once console.log (arr.length); arr.shift (0); / / delete 1 element console.log (arr.length)

Output result:

Thank you for reading this article carefully. I hope the article "whether the array length in JavaScript can be changed" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Array length element article attribute method value change one-time two value interest can be passed at the same time tutorials more friends environment computer knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB Shulou Technology Redmi Huawei