I would love to understand how exactly closure happens in this set of code and the way its implemented. I was watching a tutorial and the guy was giving example on callback function and he also gave example of closure there.
function attachEventListeners() {
let count = 0
document.getElementById("clickMe").addEventListener("click", function () {
console.log("Button Clicked", ++count)
})
attachEventListeners()
I thought in order for a closure-based count function to work, you had to return that function and assign it to a variable. Cause otherwise with each call of the outer function, count would initialize to 0. But how is it working here?