Advertisement
karlakmkj

forEach iteration

Dec 10th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
  2.  
  3. // 3 ways to iterate over fruits below
  4. // 1. Original long way
  5. fruits.forEach(function(fruitItem){
  6.   console.log('I want to eat a ' + fruitItem);
  7. })
  8.  
  9. // 2. pass a callback using arrow syntax
  10. fruits.forEach(fruitItem => console.log('I want to eat a ' + fruitItem));
  11.  
  12. // 3. define a function beforehand to be used as the callback function
  13. function printFruit(fruitItem){
  14.   console.log('I want to eat a ' + fruitItem);
  15. }
  16. fruits.forEach(printFruit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement