DEV Community

Discussion on: For Loop in different programming languages

Collapse
 
hjfitz profile image
Harry

There's a few other ways to iterate in JavaScript:

for-of

const array = [1,2,3];
for (const num of array) {
  console.log(num);
}

forEach

const array = [1,2,3];
array.forEach(num => {
  console.log(num);
};
// or
array.forEach(console.log);

Functional programming - map

const array = [1,2,3];
array.map(console.log);
Collapse
 
hjfitz profile image
Harry • Edited

Note: forEach(console.log) will print the item, index and the array - developer.mozilla.org/en-US/docs/W...

Array#forEach((item, index, array) => { /*...*/ })
Collapse
 
kind_wizzard profile image
Unknown

There is no need to use breckets in lambda:

const array = [1,2,3];
array.forEach(num => console.log(num));