Skip to main content
added 6 characters in body
Source Link
Denys Séguret
  • 383.9k
  • 90
  • 813
  • 780

If the goal is to end the loop when ii reaches 1010 or ii reaches the end of the array, you may write it like this :

for (var i=0; i<x.length && i<10; i++) { 

In that case you might also compose it like this

for (var i=0; i<Math.min(x.length,10); i++) { 

or for better performances :

for (var i=0, n=Math.min(x.length,10); i<n; i++) { 

If the goal is to end the loop when i reaches 10 or i reaches the end of the array, you may write it like this :

for (var i=0; i<x.length && i<10; i++) { 

In that case you might also compose it like this

for (var i=0; i<Math.min(x.length,10); i++) { 

or for better performances :

for (var i=0, n=Math.min(x.length,10); i<n; i++) { 

If the goal is to end the loop when i reaches 10 or i reaches the end of the array, you may write it like this :

for (var i=0; i<x.length && i<10; i++) { 

In that case you might also compose it like this

for (var i=0; i<Math.min(x.length,10); i++) { 

or for better performances :

for (var i=0, n=Math.min(x.length,10); i<n; i++) { 
added 136 characters in body
Source Link
Denys Séguret
  • 383.9k
  • 90
  • 813
  • 780

If the goal is to end the loop when i reaches 10 or i reaches the end of the array, you may write it like this :

for (var i = 0;i=0; i<x.length && i<10; i++) { 

In that case you might also compose it like this

for (var i=0; i<Math.min(x.length,10); i++) { 

or for better performances :

for (var i=0, n=Math.min(x.length,10); i<n; i++) { 

If the goal is to end the loop when i reaches 10 or i reaches the end of the array, you may write it like this :

for (var i = 0; i<x.length && i<10; i++) { 

If the goal is to end the loop when i reaches 10 or i reaches the end of the array, you may write it like this :

for (var i=0; i<x.length && i<10; i++) { 

In that case you might also compose it like this

for (var i=0; i<Math.min(x.length,10); i++) { 

or for better performances :

for (var i=0, n=Math.min(x.length,10); i<n; i++) { 
Source Link
Denys Séguret
  • 383.9k
  • 90
  • 813
  • 780

If the goal is to end the loop when i reaches 10 or i reaches the end of the array, you may write it like this :

for (var i = 0; i<x.length && i<10; i++) {