To clear a localStorage data on browser close, you can use the window.onunload event to check for tab close.
Let's say you have a local storage object called MyStorage as a global for the sake of this example. Then you can write an event handler −
Example
window.onunload = () => {
// Clear the local storage
window.MyStorage.clear()
}This will clear the local storage on the tab/window close.