Returns an integer value that is 1 larger than the subscript of the highest bit element defined in the array.
Numvar = Arrayobj.length
Parameters
Numvar
Required option. Any number.
Arrayobj
Required option. Any Array object.
Description
Because the elements in an array are not necessarily contiguous, the length property is also not necessarily equal to the number of elements in the array. For example, in the following array definition, the My_array.length contains 7 instead of 2:
code is as follows |
copy code |
var arr=[ 12,23,5,3,25,98,76,54,56,76]; //defines a 10-digit array alert (arr.length); //Displays the length of the array arr.length=12; //increase the length of the array alert ( Arr.length); //display the length of the array has changed to Alert (arr[8]); //Display the value of the 9th element, arr.length=5; Reduces the length of the array to 5, and the elements indexed equal to or more than 5 are discarded alert (arr[8]); //display 9th element has changed to "undefined" arr.length=10; //restore array length to Alert (arr[8]); //Although the length is restored to 10, the 9th element cannot be retracted, displaying "undefined" |
From the above code we can clearly see the nature of the length property. But the length object can be set not only explicitly, it may also be implicitly modified. You can use a variable that is not declared in JavaScript, and you can use an undefined array element (an element whose index exceeds or equal to length), at which point the value of the length property is set to the value plus 1 for the element index used. For example, the following code:
The code is as follows |
Copy Code |
var arr=[12,23,5,3,25,98,76,54,56,76]; alert (arr.length); arr[15]=34; alert (arr.length); |
Let's look at a piece of code:
The code is as follows |
Copy Code |
var arr = new Array (); Arr["a"] = ' a '; document.write (arr.length); Output 0 Arr[-1] = ' B '; document.write (arr.length); Output 0 ARR[2] = 1; document.write (arr.length); Output 3 for (var i = 0; I < arr.length; i++) { document.write (Arr[i] + "T"); } Output Undefine undefine 1
|
The first two outputs can see that elements with strings and negative numbers are not recorded in the length of the array. Unlike the PHP array, the length of the array in JavaScript records only the elements labeled positive integers, and the length of the array equals the value of the object under the maximum array plus 1 (the range of the array subscript is 0~2^32-2), The arr[' A ' in the code above is equivalent to ARR.A
To traverse an array element, you use the For in method
The code is as follows |
Copy Code |
for (var x in arr) { document.write (x+ "|") + arr[x]+ "T"); }//output a|a-1|b 1|1
|