Three ways to jump out of the loop in js

Break out of the loop:

1. continue; jump out of the current cycle and continue to the next cycle;

function ceshi(){
    
    
  for(var i = 0 ; i < 6 ; i++){
    
    
    if(i == 3){
    
    
      continue;
    }
    console.log(i);
  }
}

Output result: 1,2,4,5,6

2. break; jump out of the current cycle, that is, not in this cycle;

If multiple for loops are nested, the outer loop will not be affected;


function ceshi(){
    
    
  for(var i = 0 ; i < 6 ; i++){
    
    
    if(i == 3){
    
    
      break;
    }
    console.log(i);
  }
}

Output result: 1,2

3. return; end function call;

function ceshi(){
    
    
  for(var i = 0 ; i < 6 ; i++){
    
    
    for(var j = 0 ; j < 6 ; j++){
    
    
    	if(j == 2){
    
    
        	return j;
      }
  	}
  }
}
console.log(ceshi());

Output result: 2

Guess you like

Origin blog.csdn.net/qq_44749901/article/details/128134916