Skip to content

Instantly share code, notes, and snippets.

@iandesj
Created October 11, 2019 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandesj/efc00d4ad9a5b4afa657b2894b0f803c to your computer and use it in GitHub Desktop.
Save iandesj/efc00d4ad9a5b4afa657b2894b0f803c to your computer and use it in GitHub Desktop.
Examples of foreach() vs for loop
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