Tutorialspoint

ES6 Label with Break

outerloop: // This is the label name  
for (var i = 0; i < 5; i++) {  
   console.log("Outerloop: " + i);  
   innerloop:  
   
   for (var j = 0; j<5; j++) {  
      if (j>3 ) break; 
      
      // Quit the innermost loop  
      if (i == 2) break innerloop; 
      
      // Do the same thing  
      if (i == 4) break outerloop; // Quit the outer loop  
      console.log("Innerloop: " + j);  
   }  
}
 

ES6 Continue Statement

var num = 0;
var count = 0; 
for(num = 0;num <= 20;num++) { 
   if (num % 2 == 0) { 
      continue; 
   } 
   count++; 
} 
console.log(" The count of odd values between 0 and 20 is: "+count)

ES6 for-of loop

var i = 1 
while(i <= 10) { 
   if (i % 5 == 0) {   
      console.log("The first multiple of 5  between 1 and 10 is : "+i) 
      break     //exit the loop if the first multiple is found 
   } 
   i++ 
}

ES6 Do-While Loop Example

var n = 10;   
do { 
   console.log(n); 
   n--; 
} 
while(n >= 0); 

ES6 for-of loop

var num = 5; 
var factorial = 1; 

while(num >= 1) { 
   factorial = factorial * num; 
   num--; 
} 
console.log("The factorial  is "+factorial);

ES6 While Loop

var num = 5; 
var factorial = 1; 

while(num >= 1) { 
   factorial = factorial * num; 
   num--; 
} 
console.log("The factorial  is "+factorial);

ES6 for-of loop

for (let val of[12 , 13 , 123]) {   
   console.log(val) 
}

ES6 for-in Loop

var obj = {a:1, b:2, c:3};  
for (var prop in obj) { 
   console.log(obj[prop]); 
}

ES6 ‘for’ Loop

"use strict" 
for(let temp, i = 0, j = 1; j<30; temp = i, i = j, j = i + temp) 
console.log(j);

ES6 Switch Case Statement

var grade = "A"; 
switch(grade) { 
   case "A": { 
      console.log("Excellent"); 
      break; 
   } 
   case "B": { 
      console.log("Good"); 
      break; 
   }
   case "C": { 
      console.log("Fair"); 
      break;    
   } 
   case "D": { 
      console.log("Poor"); 
      break; 
   }  
   default: { 
      console.log("Invalid choice"); 
      break;              
   } 
} 

1 2 3 4 5 6 7 ... 1926 Next
Advertisements
Loading...