SyntaxStudy
Sign Up
JavaScript Storage Capacity and Limits
JavaScript Intermediate 4 min read

Storage Capacity and Limits

Storage Limits

localStorage has a limit of approximately 5MB per origin. Exceeding it throws a QuotaExceededError. Check available space and handle errors gracefully.

Example
// Handle quota exceeded
function safeStore(key, value) {
  try {
    localStorage.setItem(key, JSON.stringify(value));
    return true;
  } catch (e) {
    if (e.name === "QuotaExceededError") {
      console.warn("Storage full — clearing old cache");
      localStorage.clear();
      return false;
    }
    throw e;
  }
}

// Estimate storage usage (Chrome only)
const estimate = await navigator.storage.estimate();
console.log(`Used: ${estimate.usage} / ${estimate.quota} bytes`);
Pro Tip

5MB sounds like a lot, but large JSON objects fill it fast — store only what you truly need in localStorage.