r/learnjavascript • • Oct 24 '26

Some notes on callbacks

I've written some notes on callbacks as part of my effort to write tests and be methodical in learning JavaScript.

Callbacks are one of those things that are quite easy once you go "Aha", but like probably most people, I found them very confusing at first.

12 Upvotes

9 comments sorted by

1

u/FreeLogicGate Oct 24 '26

Is it "quiet" easy, or "quite" easy?

1

u/RobGoLaing Oct 24 '26

Thanks, I've edited the typo.

3

u/DirtAndGrass Oct 24 '26

It's worth noting that callbacks are not a JavaScript construct, they are a technique made possible by being able to reference functions.

And promises should be used instead of callbacks 

1

u/Plenty-Appointment91 Oct 24 '26

You know you can stylize the page right?

4

u/rupertavery64 Oct 24 '26

Nah, less is more

1

u/Cool-Bus-6028 Oct 24 '26

Looks great to me.

But I guess it could do with marquee or two and some dancing hamsters.

1

u/senocular Oct 24 '26

FWIW not all functions passed in as arguments are callbacks and not all callbacks are functions that get passed into other functions.

Web components for example have callbacks that are methods of the class used to define the component, ex:

class MyWebComponent extends HTMLElement {
  connectedCallback() {
    // called when web component connected to DOM
  }
}

And addEventListener also accepts objects as listeners, where the callback is a method of the object rather than being the argument itself.

const obj = {
  handleEvent(event) {
    console.log("Clicked!")
  }
}
addEventListener("click", obj)
dispatchEvent(new Event("click")) // Clicked!