Last active
November 25, 2017 15:57
-
-
Save idettman/0e5011a640ec1fefd6e0 to your computer and use it in GitHub Desktop.
JavaScript - for iteration advanced tips and examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (var i=0, each; each = arr[i]; i++) { | |
doSomething(each); | |
} | |
for (var i=arr.length; i--;) { | |
var member = arr[i]; | |
doSomething(member); | |
} | |
function sum(arr) { | |
for (var i=arr.length, r=0; i--; r += arr[i]); | |
return r; | |
} | |
sum([3,5,0,-2,7,8]); //21\ | |
function shoutOutSum(arr, x) { | |
for (var i=arr.length, r=0; i-- || alert(r); r += arr[i]); | |
} | |
shoutOutSum([3,5,0,-2,7,8]); //alerts "21" | |
var i = 0; | |
for ( | |
console.log('start:',+new Date); | |
i<1000 || console.log('finish:',+new Date); | |
i++ | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment