JavaScript For 循环示例

javascript for 循环示例

标准 for 循环

for (let i = 0; i 



<h2>
  
  
  for...of 循环(遍历数组)
</h2>



const fruits = ['apple', 'banana', 'orange'];
for (const fruit of fruits) {
  console.log(fruit);

for...in 循环(迭代对象属性)

const person = {
  name: 'john',
  age: 30,
  job: 'developer'
};

for (const key in person) {
  console.log(`${key}: ${person[key]}`);
}

foreach循环(数组方法)

const numbers = [1, 2, 3, 4, 5];
numbers.foreach((number, index) => {
  console.log(`index ${index}: ${number}`);
});

while 循环(严格来说不是 for 循环,但值得一提)

let count = 0;
while (count 



<p>这些示例演示了在 javascript 中进行迭代的各种方法,每种方法都适合不同的场景和数据结构。</p>


          

            
        

以上就是JavaScript For 循环示例的详细内容,更多请关注其它相关文章!