Exercise 10: Storage Event Listener

Problem Statement

Function `onStorageChange(callback)` banao jo storage changes ko listen kare. Example: onStorageChange((key, newValue) => console.log(key, newValue))

Sample Output:

onStorageChange((key, newValue) => console.log(key, newValue))
// Listens to storage changes

Solution

const onStorageChange = (callback) => {
  window.addEventListener("storage", (e) => callback(e.key, e.newValue));
};

Explanation

Overall Goal:

  • Storage changes ko listen karna.

Real world:

  • Multi-tab: cross-tab updates.
  • Sync: storage synchronization.