How to use the three types of for loops in JavaScript ES6

2 Answers

0 votes
function for_examples() { 
    var obj = {1:"abc", 2:"xyz", 3:"uvw", 4:"rst"}; 
  
    for(let i = 0 ; i <= 5; i++) { 
        console.log(i) 
    } 
    
    console.log('\n') 
    for (const e of ['node.js', "javascript", 123, 3.14]) { 
        console.log(e)  
    } 
    
    console.log('\n') 
    for (const key in obj) { 
        if (obj.hasOwnProperty(key)) {           
            console.log(key, obj[key]);
        }
    } 
} 

for_examples(); 

 
 
 
      
/*
run:
    
0
1
2
3
4
5


node.js
javascript
123
3.14


1 abc
2 xyz
3 uvw
4 rst
  
*/

 



answered Mar 24, 2020 by avibootz
0 votes
function for_examples() { 
    var obj = {1:"abc", 2:"xyz", 3:"uvw", 4:"rst"}; 
   
    for(let i = 0 ; i <= 6; i++) { 
        console.log(i) 
        if (i == 4) {
            break;
        }
    } 
     
    console.log('\n') 
    for (const e of ['node.js', "javascript", 123, 3.14]) { 
        if (e == 123) {
            continue;
        }
        console.log(e)  
    } 
     
    console.log('\n') 
    for (const key in obj) { 
        if (key == 3) {
            return;
        }
        if (obj.hasOwnProperty(key)) {           
            console.log(key, obj[key]);
        }
    } 
} 
 
for_examples(); 
  
  
  
       
/*
run:
     
0
1
2
3
4


node.js
javascript
3.14


1 abc
2 xyz
   
*/

 



answered Mar 24, 2020 by avibootz

Related questions

2 answers 192 views
1 answer 166 views
1 answer 153 views
2 answers 173 views
3 answers 205 views
...