Home > Web Front-end > JS Tutorial > How to determine the length of an array in js

How to determine the length of an array in js

青灯夜游
Release: 2023-01-03 09:26:30
Original
15847 people have browsed it

In JavaScript, you can use the length attribute to determine and return the length of an array. The syntax format is "arrayObject.length"; the length attribute can return the maximum length of the array, that is, its value is equal to the maximum subscript value of the array plus 1.

How to determine the length of an array in js

The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.

The length property sets or returns the number of elements in an array.

Each array has a length attribute, which returns the maximum length of the array, that is, its value is equal to the maximum subscript value plus 1. Since the numeric subscript must be less than 2^32-1, the maximum value of the length attribute is equal to 2^32-1.

Syntax

arrayObject.length
Copy after login

Example 1

The following code defines an empty array, and then assigns a value to the element with the subscript equal to 100, then the length attribute returns 101. Therefore, the length attribute cannot reflect the actual number of array elements.

var a = [];  //声明空数组
a[100] = 2;
console.log(a.length);  //返回101
Copy after login

The length property is readable and writable and is a dynamic property. The length attribute value is also automatically updated as the array elements change. At the same time, if the length attribute value is reset, it will also affect the elements of the array. The specific instructions are as follows:

  • If the length attribute is set to a value smaller than the current length value, the array will Truncated, element values ​​beyond the new length will be lost.

  • If the length attribute is set to a value greater than the current length value, then the empty array will be added to the end of the array, making the array grow to the newly specified length, and read the value All are undefined.

Related recommendations: JavaScript video tutorial

Example 2

The following code demonstrates the length attribute The impact of dynamic changes in values ​​on arrays.

var a = [1,2,3];  //声明数组直接量
a.length = 5;  //增长数组长度
console.log(a[4]);  //返回undefined,说明该元素还没有被赋值
a.length = 2;  //缩短数组长度
console.log(a[2]);  //返回undefined,说明该元素的值已经丢失
Copy after login

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to determine the length of an array in js. 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