How to delete a localStorage item when the browser window/tab is closed?
Use with window
global keyword:-
window.localStorage.removeItem('keyName');
You should use the sessionStorage instead if you want the key to be deleted when the browser close.
should be done like that and not with delete operator:
localStorage.removeItem(key);