In JavaScript, when you reference a variable, it will first look in the local scope before checking the global scope. In your browser, all globally scoped variables are properties of the window object. Looking up window.event yields the following article: https://developer.mozilla.org/en-US/docs/Web/API/Window/event. Notably:
The read-only Window property event returns the Event which is currently being handled by the site's code. Outside the context of an event handler, the value is always undefined.
This means that any time you are currently handling an event, the browser also binds that event to the global scope.
It should be noted that, as mentioned in the article, actually taking advantage of this is a bad idea. The value is not always what you would expect, and using global variables in general is not preferred.
As for your second question: Any parameter of any callback function can be omitted as long as you don't plan to use it in your handling, and if you do include it you can name it what you like. In this case, it just so happens that there is a global variable that has the same name as the name commonly used for that parameter. The exception to this is that you do have to define all previous parameters to access later ones.
Some examples:
// This is OK - you don't have to call the passed event "event"
buttonElement.addEventListener('click', function (ev) {
alert(ev.type);
});
// This is not OK - the global variable is named "event" so "ev" in this case is undefined
buttonElement.addEventListener('click', function () {
alert(ev.type);
});
// This is OK but not preferred because it uses the global "event" variable
buttonElement.addEventListener('click', function () {
alert(event.type);
});
// This is fine - `event` overrides (shadows) the global variable so the local reference gets used
buttonElement.addEventListener('click', function (event) {
alert(event.type);
});
// This the same as the preceding example
buttonElement.addEventListener('click', function () {
alert(window.event.type);
});
handleEventfunction signature?undefined).