Skip to content

Instantly share code, notes, and snippets.

@omarbenmegdoul
Created March 27, 2021 01:07
Show Gist options
  • Save omarbenmegdoul/f3c84b302ccf67c225768df516161b0e to your computer and use it in GitHub Desktop.
Save omarbenmegdoul/f3c84b302ccf67c225768df516161b0e to your computer and use it in GitHub Desktop.
someArray.forEach(callback) explainer
//This program will log all elements in an array to the console.
//forEach is used for DOING something with each element of an array.
//Notice that we're not trying to produce a value: we're not getting back a modified array or even a single value at the end. There is no output, just logging.
//If you want, you can define the function directly inside the `baseArray.forEach(callback)` call. Literally cut and paste.
let baseArray = [1,2,3,4,5,6,7,8,9,0];
baseArray.forEach(
function logger(someMessage) {
console.log(someMessage); //this is the DOING something part
}
)
//note that since forEach has no output (no return statement) we don't write the following:
//finalOutput = baseArray.forEach(...etc...
//end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment