Advertisement
Intermediate JavaScript, Browser Storage & Web APIs

What is the storage event and when does it fire?

Short answer

The storage event fires on window in OTHER tabs/windows of the same origin whenever localStorage changes — it never fires in the tab that made the change itself. It's useful for keeping multiple open tabs in sync.

Save JavaScript mock interview
Advertisement
AModel answer
The storage event fires on window in OTHER tabs/windows of the same origin whenever localStorage changes — it never fires in the tab that made the change itself. It's useful for keeping multiple open tabs in sync.
window.addEventListener('storage', (e) => {
  console.log(e.key, e.oldValue, e.newValue);
});
Real-world example
Logging a user out in every open tab the instant they log out in one tab.
Likely follow-ups
  • Does sessionStorage also trigger the storage event?
Related topics