js for each for of for in的代码示例
时间: 2023-11-09 19:03:28 浏览: 63
js中for in的用法示例解析
好的,以下是三种循环的代码示例:
1. for 循环
```
const arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
```
2. forEach 循环
```
const arr = [1, 2, 3, 4, 5];
arr.forEach((item) => {
console.log(item);
});
```
3. for...of 循环
```
const arr = [1, 2, 3, 4, 5];
for (const item of arr) {
console.log(item);
}
```
4. for...in 循环
```
const obj = { a: 1, b: 2, c: 3 };
for (const key in obj) {
console.log(`${key}: ${obj[key]}`);
}
```
阅读全文