Created
October 11, 2019 17:47
-
-
Save iandesj/efc00d4ad9a5b4afa657b2894b0f803c to your computer and use it in GitHub Desktop.
Examples of foreach() vs for loop
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
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ]; | |
// improved looping | |
fruits.forEach((value, index, array) => { | |
console.log('fruit at index', index, '=', value); | |
}); | |
// what you may already be used to | |
for (let i=0; i<fruits.length; i++) { | |
console.log('fruit at index', i, '=', fruits[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment