Array method: forEach() - 06

The Array method forEach() executes or runs a callback function for each array element. It returns undefined. So we cannot use any method like sort(), filter(), and reduce().

This method is not chainable. We cannot do anything with returned value from the forEach() method since it returns an undefined value. We have to do everything inside the callback function.

Syntax: 













const arr = ["mozahedul", "rashidul", "lutfor", "john", "elton"];
const result = arr.forEach((item) => {
   return item;
});

console.log(result);
// Return: undefined


const arr = ["mozahedul", "clark", "moin", "john", "elton"];
  arr.forEach((item) => {
      console.log(item);
  });

    // Result:
    mozahedul
    clark
    moin
    john
    elton


  const elem = document.querySelector(".show");

    const arr = ["mozahedul", "clark", "moin", "john", "elton"];
    arr.forEach((item) => {
      elem.innerHTML += `${item} <br/>`;
    });

    // Result:
    mozahedul
    clark
    moin
    john
    elton



Post a Comment

Previous Post Next Post