JavaScript basic learning series twenty-seven: for-in statement

The for-in statement is a strict iteration statement used to enumerate non-symbol key properties in an object. The syntax is as follows:

for (property in expression) statement

Below is an example:

for (const propName in window) {
    
    
  document.write(propName);
}

This example uses a for-in loop to display all properties of the BOM object window. Each time the loop is executed, the variable 13 propName will be assigned a property of the window object as a value until all properties of the window have been enumerated. As with the for loop, the const in the control statement is not required here. But in order to ensure that this local variable is not modified, it is recommended to use const.

The properties of objects in ECMAScript are unordered, so the for-in statement cannot guarantee the order in which the object properties are returned. All enumerable properties are returned once, but the order in which they are returned may vary between browsers.

If the variable to be iterated by the for-in loop is null or undefined, the loop body is not executed.

1. for-of statement:

The for-of statement is a strict iteration statement used to iterate over the elements of an iterable object. The syntax is as follows: for (property of expression) statement
Here is an example:

    for (const el of [2,4,6,8]) {
    
    
      document.write(el);
}

In this example, we use the for-of statement to display all the elements in an array of 4 elements. The loop will continue until all elements have been iterated. As with the for loop, the const in the control statement is not required here. But in order to ensure that this local variable is not modified, it is recommended to use const.

The for-of loop iterates over the elements in the order in which the next() method of the iterable object produces values. If the variable you are trying to iterate over does not support iteration, the for-of statement throws an error.

2. Label statement:

Label statements are used to label statements. The syntax is as follows:

    label: statement

Below is an example:

   start: for (let i = 0; i < count; i++) {
    
    
      console.log(i);
}

In this example, start is a label that can be referenced later by a break or continue statement. A typical application scenario for label statements is nested loops.

3. break and continue statements:

The break and continue statements provide tighter control over the execution of looping code. Among them, the break statement is used to exit the loop immediately and force the execution of the next statement after the loop. The continue statement is also used to exit the loop immediately, but starts execution from the top of the loop again.

In the above code, the for loop will increment the variable i from 1 to 10. Within the loop body, there is an if statement that checks whether i is divisible by 5 (using the modulo operator). If so, execute the break statement and exit the loop. The initial value of the variable num is 0, which indicates how many times the loop is executed before exiting.

When the break statement is executed, the next line of code executed is console.log(num), which displays 4. The reason why the loop is executed 4 times is because when i equals 5, the break statement will cause the loop to exit, and the loop will not execute the code that increments num. If break is replaced by continue, different effects will occur.

let num = 0;
    for (let i = 1; i < 10; i++) {
    
    
      if (i % 5 == 0) {
    
    
continue;
}
num++; }
    console.log(num); // 8

This time, the console.log shows 8, which means the loop has been executed completely 8 times. When i equals 5, the loop exits before incrementing num, but executes the next iteration, where i is 6. The loop then executes until its natural end, when i equals 10. The final value of num is 8 instead of 9 because the continue statement causes it to be incremented one less time.

Both break and continue can be used with label statements to return to a specific location in the code. This is usually within a nested loop, as shown in the example below.

let num = 0;
num++; 10
 outermost:
for (let i = 0; i < 10; i++) {
    
    
 for (let j = 0; j < 10; j++) {
    
    
  if (i == 5 && j == 5) {
    
    
break outermost;
 } }
console.log(num); // 55

Guess you like

Origin blog.csdn.net/wanmeijuhao/article/details/135464842