Break loop once condition match JavaScript

const arr = [1,2,3]; 

Iterations loops:
for 
every() 
some()

We can use:
for(let item of arr){ 
 console.log(item);
 if(item == 2){
     break; 
 } 
}

Or

for (var i = 0; i < arr.length; i++) { 
 console.log(arr[i]); 
 if (arr[i] === 2) {
       break;
  } 
}

Or

arr.every((elm) => { 
 console.log(elem); 
 if (elm === 2) {
       return true;
 }
 return false;
});

Or 

arr.every((elm) => { 
 console.log(elem); 
 if (elm === 2) {
       return false;
 }
 return true; 
});

Comments

Popular posts from this blog

world tourism guide Template

Es6 how to check if value is not null or undefined?