Make a function call before any trigger in Javascript

I want to make a button using onclick handler that will call the function once before any trigger.

In React I can do like this

<button onClick={clickHandler()}></button>

Is it possible to do the same thing in pure Javascript?

Answer

Yes, the function just have to return another one.

let button = /* your button */;
button.addEventListener('click', (function(){
    /* logic before any trigger */
    return function(){
        /* logic for every trigger */
    }
})());

Alternatively, you just run whatever code before attaching the handler:

let button = /* your button */;
/* logic before any trigger */
button.addEventListener('click', function(){
    /* logic for every trigger */
});

Same logic for both scenarios:

let button = /* your button */;
function myButtonLogic(){
   /* logic for everything */
}
myButtonLogic();
button.addEventListener('click', myButtonLogic);