You can use global variable outside then use it like:
let variable = '';
console.log('before eventlistener', variable);
document.querySelector('button#first').addEventListener('click', () =>{
variable = 'ei';
console.log('on eventlistener', variable);
});
function printMe(){
console.log('before eventlistener', variable);
}
<button id='first'>first button</button>
<button onclick='printMe();'>second button</button>
As you can press the first button will set variable, press the second will call new value of that. (if you press instead the second button without press the first you will see empty variable)