Example:
const arr = ['cat', 'dog', 'fish'];
arr.forEach(element => {
console.log(element);
});
let text = '';
const fruits = ['apple', 'orange', 'cherry'];
fruits.forEach(myFunction);
function myFunction(item, index) {
console.log('index : '+index+' , item : '+item);
}
For-in loop
For-in is used to iterate over the enumerable properties of objects. Every property in an object will have an Enumerable value — if that value is set to true, then the property is Enumerable.
for (variable in object) {
// do something
}
Example:
const obj = {
a: 1,
b: 2,
c: 3,
d: 4
}
for (let elem in obj) {
console.log( obj[elem] )
}