I was learning javascript event handling when I ran into something weird.
When I wrote my onclick event listener as
.addEventListener("click", alert('button clicked'))
it alerted immediately
searching online the fix seems to be to wrap it inside of a function like this
addEventListener("click", function(){ alert('button clicked')}
It was quite confusing since it looks like both versions bound the click to an action. Except the second one has an extra layer wrapped around it.
Is the reason for this behavior because js is compiling and running the line immediately after it read it? But then wouldn't the function also run as well?