var array_name = new Array(); // By using the new keyword var array_name = [value1, value2,....valueN]; //By using Array literals or, var array_name; //Declaration array_name=[value1, value2,…..valueN]; //Initialization
array_name[subscript];
var num; num = [2,4,6,8]; console.log(num[0]); console.log(num[1]);
2 4
以逗号或, 分隔的值列表
表示数组大小的数值
var num = new Array(5); // this single numeric value indicates the size of array. var i; for(i=0;i<num.length;i++){ num[i]=i*5; console.log(num[i]); }
0 5 10 15 20
var num = new Array(1,2,3,4,5); var i; for(i=0;i<num.length;i++){ console.log(num[i]); }
1 2 3 4 5
多维数组
将数组传递给函数
从函数返回数组
var array_name = [[value1,value2,value3],[val1,val2,val3]];
var array_name[initial_array_index][referenced_array_index]
var multi = [[2,3,4],[4,9,16]] console.log(multi[0][0]) console.log(multi[0][1]) console.log(multi[0][2]) console.log(multi[1][0]) console.log(multi[1][1]) console.log(multi[1][2])
2 3 4 4 9 16
var rainbow = new Array["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"]; function show(rainbow) { for(var i = 0;i<rainbow.length;i++) { console.log(rainbow[i]) } } show(rainbow)
Violet Indigo Blue Green Yellow Orange Red
function show() { return new Array("Blue", "Red", "Green", "Yellow") } var colors = show() for(var i in colors) { console.log(colors[i]) }
Blue Red Green Yellow