The forEach()
of an array instance is used to execute a function, which we call “callback function”, for all elements of an array. This callback function is passed the current item:
const list = [1, 2, 3]
list.forEach(element => {
console.log(element * 2)
})
It’s similar to map()
, however nothing is returned from forEach
while using map
you get a new array with the result of the function executed in the callback.
You can also get the index of the element as the second parameter to the callback function:
const list = [1, 2, 3]
list.forEach((element, index) => {
console.log(element, index)
})
Lessons in this unit:
0: | Introduction |
1: | map() |
2: | filter() |
3: | reduce() |
4: | sort() |
5: | find() and findIndex() |
6: | ▶︎ forEach() |
Are you intimidated by Git? Can’t figure out
merge vs rebase? Are you afraid of screwing up
something any time you have to do something in
Git? Do you rely on ChatGPT or random people’s
answer on StackOverflow to fix your problems?
Your coworkers are tired of explaining Git to
you all the time? Git is something we all need
to use, but few of us really master it. I
created this course to improve your Git (and
GitHub) knowledge at a radical level. Launching
May 21, 2024. Join the waiting list!