1.用for循环遍历
<script>
cars=["BMW","Volvo","Saab","Ford"];
cars[10]=["B"]
for (var i=0,l=cars.length; i<l; i++){
document.write(cars[i] + "<br>");
}
</script>
2.用forEach方法
第一种写法
<script>
cars=["BMW","Volvo","Saab","Ford"];
cars[10]=["C"]
cars.forEach(element => document.write(element + "<br>"));
</script>
第二种写法
<script>
cars=["BMW","Volvo","Saab","Ford"];
cars[10]=["C"]
var res = cars.forEach(function(item,index) {
document.write(`数组第${index+1}个元素是${item}`+ "<br>");
})
</script>