第一种方式(不推荐):
let arr1=new Array();
arr1[0]=123;
arr1[1]="abc";
console.log(arr1[0]);
console.log(arr1[2]);//超过长度不会报错而是返回undefined
console.log(arr1.length);
console.log(arr1);
第二种方式(不推荐):
let arr6=new Array("123","aaa","bbb");
第三种(推荐):数组字面量形式(和对象花括号不同,数组用的是中括号)
let arr2=[123,456,"aaa","bbb"];
console.log(arr2);
数组也可以存放对象(通过对象字面量)
let arr4=[
{name:"zhangsan",age:17},
{name:"lisi"},
]
console.log(arr4[0],arr4[1]);
数组也可以存放函数:
let arr5 = [
function () {
console.log("test1");
},
function () {
document.write("hello");
}
]
arr5[0]();
arr5[1]();
查找:
let a=arr1.indexOf(123);
console.log(a);
let b=arr1.indexOf("12345678");
console.log(b);//没找到返回-1