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:mozahedulclarkmoinjohnelton
const elem = document.querySelector(".show");const arr = ["mozahedul", "clark", "moin", "john", "elton"];arr.forEach((item) => {elem.innerHTML += `${item} <br/>`;});// Result:mozahedulclarkmoinjohnelton
Post a Comment