创建并输出数组

var arr = [];

var arr2 = [1,true,false,null,undefined,"haha",[7,8]];

console.log(arr2);

2023-06-28T14:13:54.png

获取数组

var arr = [];

var arr2 = [1,true,false,null,undefined,"haha",[7,8]];

console.log(arr2);


console.log(arr2[0]);
console.log(arr2[3]);
console.log(arr2[5]);

2023-06-28T14:19:15.png

数组长度

        //空数组
        var e = [];
        //为数组赋值
        var w = [1,true,false,null,undefined,"true",[8,1]];
        //获取数组内数值的数量
        console.log(w.length);
        //获取数组内数值的数量并减1
        console.log(w.length - 2);
        //将第10个数值改为555
        w[10] = 555;
        //获取数组内数值的数量
        console.log(w[10]);
        //将数组内数值数量改为50
        w.length = 50;
        //获取数组内数值的数量
        console.log(w.length)
        //将数组内数值数量改为2个
        w.length = 2;
        //获取数组内数值的数量
        console.log(w.length);

2023-06-28T23:10:13.png

循环获取数值并输出

var e = [123,122,311,47,23,76,54,99,30];

for(var i = 0; i <= e.length - 1; i++){

    console.log(e[i]);
}

2023-06-28T23:21:09.png

循环为数值加10并赋值

var e = [123,122,311,47,23,76,54,99,30];

for(var i = 0; i < e.length; i++){

    e[i] += 10;

}
console.log(e);

2023-06-28T23:27:38.png