In javascript, the array length is very simple. You can use the length function to obtain the specific array length. However, when using the group length traversal function, it should be length-1 and the starting value is 0.
Array length:
You do not need to set the length of the javascript array. The array name. length returns the number of elements.
The Code is as follows: |
Copy code |
Var arr = new Array ("Saab", "Volvo", "BMW ") Alert (arr. length) // you can display the array length. |
The above is not our focus. Let's take a look at it below.
First:
The Code is as follows: |
Copy code |
Var lennon = Array (); Lennon ["name"] = "windy "; Lennon ["year"] = "1989 "; Lennon ["living"] = true; Alert (lennon. length ); |
Result: lennon. length is 0.
Cause:
Add an array element without changing the length of the array.
Arr ["a"] = 3;
Second point:
The Code is as follows: |
Copy code |
Var foo2 = []; Foo2 ['3'] = 3; Foo2 ['age'] = 20; Debugger; Alert (foo2.length ); |
Foo2.length is 4 again
All array objects without assignment will be defined as undefined.
JavaScript can automatically re-adjust the array size. This automatic size adjustment mechanism makes it easier to see out of bounds arrays and is more difficult to find out. Pay more attention to similar situations.