According to MDN: The beforeunload event is fired when the current window, contained document, and associated resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.
If the user wants to close or refresh the tab, window, or browser, preventing that from happening would be annoying for users. This is because is no way to prevent users from doing this.