zach.codes

Share this post

User's avatar
zach.codes
Callbacks
Copy link
Facebook
Email
Notes
More

Callbacks

Hard to grasp in the beginning...

Zach Silveira's avatar
Zach Silveira
Oct 18, 2022
∙ Paid

Share this post

User's avatar
zach.codes
Callbacks
Copy link
Facebook
Email
Notes
More
Share

Recently I was answering questions from Mike, and we got into callback functions.

He had some code like this:

for(let i=0;i < array.length; i++) {
 //do something
}

This lead me to dive into Array.map and Array.forEach. The point of bringing those up, was to explain that you don’t normally have to write a raw “for” loop. Usually you will be iterating over an array, and you are able to utilize these helper methods to do this much faster.

array.forEach(item => console.log(item))

This single line of code took a lot of explaining. I try really hard to break out everything into variables when discussing new paradigms to new developers. The line above became:

let array = [1,2,3]

function forEachItem(item) {
  console.log(item)
}

array.forEach(
  forEachItem
)

Discussing how parentheses “call” or “invoke” functions was very helpful to Mike.

It’s a hard to comprehend passing a function as a variable, to another function.

In this case, we are passing the function forEachItem to the array.forEach func…

Keep reading with a 7-day free trial

Subscribe to zach.codes to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Zach Silveira
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More