How to delete a localStorage item when the browser window/tab is closed?

Solution 1:

should be done like that and not with delete operator:

localStorage.removeItem(key);

Solution 2:

Use with window global keyword:-

 window.localStorage.removeItem('keyName');

Solution 3:

You should use the sessionStorage instead if you want the key to be deleted when the browser close.