The importance of arrays in programming languages is self-evident. Arrays are also one of the most commonly used objects in JavaScript. Arrays are ordered collections of values. Due to weak types, arrays in JavaScript are very flexible and powerful. Unlike strongly typed high-level language arrays such as Java can only store elements of the same type or its subtypes. JavaScript can store multiple types of elements in the same array, and the length can also be dynamically adjusted. The length of the array can be automatically changed as the data increases or decreases.
First, let’s talk about the basic usage of arrays.
Arrays, that is, Array type, are one of the most commonly used types in development. The biggest difference between arrays in javascript and other languages is that each item can save any type of data, and the size of the array can be adjusted dynamically , which is a bit tangible? Look at the code
1.Array creation:
var arr=new Array(20);var arr1=["small umbrella",1,true,undefined,[2,"king"],""]
The first line uses the array construction method to create an array with a length value of 20, and the second line uses the array literal representation to create an array with multiple data types coexisting.
The method on the first line is a bit of a pitfall. The brackets specify the length of the array, rather than the first element of the array being 20. If you want to create an array with an element being 20, you should use the literal method to create it.
There is another small pit, look at the code
var arr=[1,2,];var arr1=[,,,,]
The first line will create an array with values of 1, 2 and undefined in IE8 and previous versions. Other browsers will generate an array with two items of 1, 2 respectively.
The second line may also contain 5 or 6 items on different browsers. (IE9 and above fix this problem, but this writing method is still not recommended)
2. Basic operations of array elements
JS arrays are very flexible and have many methods of operating array elements, but there are also some small pitfalls, such as:
var arr=[];arr[0]=1;arr[1]=2;arr.push(3); //arr=[1,2,3]arr[arr.length]=4; //arr=[1,2,3,4]arr.unshift(0); //arr=[0,1,2,3,4];delect arr[2]; //arr=[0,1,undefined,3,4]arr[0]=undefined //arr=[undefined,1,undefined,3,4]
Push() method will add an element to the last edge of the array. The resulting value can be found in the comments, ^_^
You can also manually subscript the array to expand, if the third line is actually equivalent to push(4);
Then if you want to add it at the front of the array element, don't worry, there is the unshift() method.
The corresponding pop() method and shift() method that delete elements correspond to push() and unshift() respectively, so I will not repeat it again.
The delect operator's operation on an array will not delete the elements of the array. It can be said that undefined is used to occupy a single seat, which is the same as direct assignment.
3. Sparse arrays
A sparse array is an index of an array that does not start from 0. Generally, the length of an array is more than the number of elements:
var arr1=[undefined];var arr2=new Array(1);0 in arr1; //true0 in arr2; //falsevar arr3=new Array(100);arr3[99]="assignment";99 in arr3; //true98 in arr3; //false
In arr1, it is because the first column of the array has a value. Although it is undefined, arr2 only specifies the length of the array, and the first column still has no value, so using the in statement will return false. After assigning the value to the 100th element in arr3, it detects that it has a value, and has no effect on the keys of other columns, so it still returns false.
The above is the relevant knowledge of Javascript arrays introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!