Storage: Storage size limits

Through the Storage API you can store more data than you would be able with cookies.

The current limits on desktop seem to be 10MB across browsers for both localStorage and sessionStorage, with 5MB each, per origin (per single website).

After this limit is reached, calling setItem will raise a QuotaExceededError exception, and you can intercept that using a try/catch:

try {
  localStorage.setItem('key', 'value')
} catch (e) {
  if (
    [
      'QuotaExceededError', 
      'NS_ERROR_DOM_QUOTA_REACHED' /* Firefox */
    ].includes(
      e.name
    )
  ) {
    // handle quota limit exceeded error
    console.error('Quota exceeded')
  } else {
    // another kind of exception
    throw e
  }
}

Lessons in this unit:

0: Introduction
1: Session vs local storage
2: Setting an item
3: Getting an item value
4: Removing an item
5: Clearing storage
6: ▶︎ Storage size limits