Home > Web Front-end > JS Tutorial > body text

How to use loop in js?

PHP中文网
Release: 2017-06-21 09:33:22
Original
1160 people have browsed it

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)
}
Copy after login

 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
Copy after login

2. for in loop

Used to loop a

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!