## 基本语法
下面列出了这几个遍历语法规则:
```js
for (let index = 0; index < array.length; index++) {
const element = array[index]
// ...
}
array.forEach(element => {
// ...
})
for (const key in array) {
// ...
}
for (const iterator of array) {
// ...
}
```
## 分情况讨论这几种写法的不同
### 非数字的属性
在 JavaScript 中所有的数组都是对象,这意味着你可以给数组添加字符串属性:
```js
array = ['a', 'b', 'c']
array.test = 'testing'
console.log(array) // [ 'a', 'b', 'c', test: 'testing' ]
```
如果打印,那么这个 test 也会被打印出来
在浏览器中,使用 `console.table(array)` 打印这个数组可以看到,这个对象中 test 为 index,testing 为 value;其他数组项的 index 值均为数字
![2