Create an array of 1. Literal quantity
2. New Array
var arr = new Array();var arrWithLength = new Array(100);//undefined*100var arrLikesLiteral = new Array(true,false,null,1,2,‘hi‘);//等价于[true,false,null,1,2,‘hi‘];
Array length limit range: 0~2 23 times-1.
Array Read and write
var arr = [1,2,3,4,5];arr[1];//2arr.length;//5arr[5] = 6;//可以动态添加数组长度arr.length:;//6
Array elements are dynamically deleted, without specifying the size
//数组尾部增加元素arr.push();arr[arr.length] = 4;//equal to arr.push(4);//数组头部增加元素arr.unshift(0);var = [1,2];delete arr[0];//删除索引,值变为undefinedarr[0];//undefined , 长度不变。arr.length;//20 in arr;//false,0索引已被删除,如果不执行delete,主动为arr[0]设置值为undefined,0 in arr将得到true。//数组尾部元素删除arr.pop();arr.length--;//equal to arr.pop();//数组头部元素删除arr.shift();
Array Iteration 1. Normal for loop
var i = 0,n=10;var arr = [1,2,3,4,5];for(;i<n;i++){ console.log(arr[i]);}
2. For in (not recommended)
Iterates over the properties on the prototype chain
var i = 0,n=10;var arr = [1,2,3,4,5];for(i in arr){ console.log(arr[i]);}
3. For
var i = 0,n=10;var arr = [1,2,3,4,5];for(i of arr){ console.log(i);}
4.array.prototype.foreach
var arr = [1,2,3,4,5];arr.forEach(function(value,index,arr){ console.log(value);})
The index attribute is present if the array element is considered to be undefined, such as arr[0] = undefined, if the index attribute is not present (although the array elements inside are undefined), but the size is specified for the arrays, and no value is set.
Array (i)--array creation and manipulation