Home  >  Article  >  Web Front-end  >  How to use loop in js?

How to use loop in js?

PHP中文网
PHP中文网Original
2017-06-21 09:33:221152browse

1. for loop

Four steps:

1. Set the initial value var i = 0

 2. Set the loop execution condition i < 5

 3. Execute the content in the loop body {[loop body]} wrapped part

 4. After each round of loop, our i++ accumulation operation is executed

for(var i = 0;i<5;i++){
    console.log(i)
}

 break/continue: in the loop When these two keywords are encountered in the body, the subsequent code in the loop body will no longer be executed

Break: In the loop body, if break appears, the entire loop will end directly , the last cumulative operation of i++ is not executed either

Continue: In the loop body, if continue appears, the current round of the loop ends and the next round of loop continues. , i++ continues to execute 

for(var i = 0;i<10;i++){if(i<=5){
        i+=2;continue;
    }
    i+=3;break;
    console.log(i)//不执行}

console.log(i)//9

2. for in loop

Used to loop a

 object =&#39;小李&#39;18&#39;170cm&#39;&#39;敲代码&#39;

for(var key in object){

 console.log(key);// The attribute name obtained in each loop

Console.log(object[key])// Obtaining the attribute value in for in can only be obtained through the object name[key] and cannot write obj.key

}

Case: changing the color of every other row in the table (the ternary operator satisfies the condition and if there are multiple executions, you can add parentheses and separate them with commas)




    
    Document
    


    
        
                
  • 11111111111111111111111111
  •             
  • 22222222222222222222222222
  •             
  • 33333333333333333333333333
  •             
  • 44444444444444444444444444
  •             
  • 55555555555555555555555555
  •             
  • 66666666666666666666666666
  •             
  • 11111111111111111111111111
  •             
  • 22222222222222222222222222
  •             
  • 33333333333333333333333333
  •             
  • 44444444444444444444444444
  •             
  • 55555555555555555555555555
  •             
  • 66666666666666666666666666
  •         
    
    

The above is the detailed content of How to use loop in js?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn