JavaScript 循环综合指南

**### JavaScript 中的循环**

以下是 JavaScript 循环的综合指南和示例:

**### **1. For 循环****

当您知道需要执行的确切迭代次数时,“for”循环是理想的选择。

**#### **语法**:**

for (initialization; condition; increment/decrement) {
  // Code to execute
}

**#### **例子**:**

for (let i = 1; i <= 5; i++) {
  console.log(`Iteration: ${i}`);
}

**解释**:

  • let i = 1 初始化循环变量 i。
  • i <= 5 在每次迭代之前检查条件是否为真。
  • i++ 在每次循环执行后增加 i。
  • **### **2. While 循环****

    当迭代次数未预先确定且取决于条件时,使用 `while` 循环。

    **#### **语法**:**

    while (condition) {
      // Code to execute
    }

    **#### **例子**:**

    let count = 0;
    while (count < 5) {
      console.log(`Count is: ${count}`);
      count++;
    }

    **解释**:

  • 只要 count < 5 为真,循环就会继续。
  • 在循环内部,count 的值在每次迭代中增加。
  • **### **3. Do-While 循环****

    `do-while` 循环确保代码块至少执行一次,即使条件为假。

    **#### **语法**:**

    do {
      // Code to execute
    } while (condition);

    **#### **例子**:**

    let number = 0;
    do {
      console.log(`Number is: ${number}`);
      number++;
    } while (number < 3);

    **解释**:

  • 循环体首先运行,打印数字。
  • 随后检查条件数 < 3,确定循环是否应继续。
  • **### **4. For-In 循环****

    `for-in` 循环用于迭代对象的属性。

    **#### **语法**:**

    for (key in object) {
      // Code to execute
    }

    **#### **例子**:**

    const person = { name: "John", age: 30, city: "New York" };
    for (let key in person) {
      console.log(`${key}: ${person[key]}`);
    }

    **

    name: John
    age: 30
    city: New York

    **解释**:

  • 键保存的是属性名称。
  • person[key] 访问相应的值。
  • **### **5. For-Of 循环****

    `for-of` 循环用于迭代可迭代对象,如数组、字符串、映射或集合。

    **#### **语法**:**

    for (variable of iterable) {
      // Code to execute
    }

    **#### **例子**:**

    const fruits = ["Apple", "Banana", "Cherry"];
    for (let fruit of fruits) {
      console.log(fruit);
    }

    **

    Apple
    Banana
    Cherry

    **解释**:

  • fruit 按顺序获取数组的每个元素。
  • ***### **6. 打破循环****

    使用“break”语句提前退出循环。

    **#### **例子**:**

    for (let i = 0; i < 10; i++) {
      if (i === 5) break;
      console.log(i);
    }

    **

    0
    1
    2
    3
    4

    ***### **7. 跳过迭代****

    使用“continue”语句跳过当前迭代。

    **#### **例子**:**

    for (let i = 0; i < 5; i++) {
      if (i === 2) continue;
      console.log(i);
    }

    **

    0
    1
    3
    4

    **### **8. 嵌套循环****

    循环可以嵌套在一起,实现多维迭代。

    例子:

    for (let i = 1; i <= 3; i++) {
      for (let j = 1; j <= 2; j++) {
        console.log(`i = ${i}, j = ${j}`);
      }
    }

    **

    i = 1, j = 1
    i = 1, j = 2
    i = 2, j = 1
    i = 2, j = 2
    i = 3, j = 1
    i = 3, j = 2

    **### **9. 无限循环****

    对于条件永远不会被评估为“假”的循环要保持谨慎。

    **#### **例子**:**

    while (true) {
      console.log("This will run forever unless stopped!");
    }

    **注意**:除非有“break”机制,否则请避免此类循环。

    **### **10. 循环遍历数组****

    `for` 和 `for-of` 循环通常与数组一起使用。

    **#### **例子**:**

    const numbers = [10, 20, 30];
    for (let i = 0; i < numbers.length; i++) {
      console.log(numbers[i]);
    }
    
    for (let num of numbers) {
      console.log(num);
    }

    **### **11. 循环字符串****

    “for-of”循环对于遍历字符串的字符也很有用。

    例子:

    const text = "Hello";
    for (let char of text) {
      console.log(char);
    }

    **嗨,我是 Abhay Singh Kathayat!**

    我是一名全栈开发人员,精通前端和后端技术。我使用多种编程语言和框架来构建高效、可扩展且用户友好的应用程序。

    请随时通过我的商务电子邮件联系我:kaashshorts28@gmail.com。