Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
- // 3 ways to iterate over fruits below
- // 1. Original long way
- fruits.forEach(function(fruitItem){
- console.log('I want to eat a ' + fruitItem);
- })
- // 2. pass a callback using arrow syntax
- fruits.forEach(fruitItem => console.log('I want to eat a ' + fruitItem));
- // 3. define a function beforehand to be used as the callback function
- function printFruit(fruitItem){
- console.log('I want to eat a ' + fruitItem);
- }
- fruits.forEach(printFruit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement