Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
The Storage API allows you to store more data than cookies do.
On desktop, the limit is typically around 10 MB per origin for both localStorage and sessionStorage (the exact limit varies by browser).
When the limit is reached, calling setItem() throws a QuotaExceededError exception, which you can catch with a try/catch block:
try {
localStorage.setItem('key', 'value')
} catch (e) {
if (
[
'QuotaExceededError',
'NS_ERROR_DOM_QUOTA_REACHED' /* Firefox */
].includes(
e.name
)
) {
// handle quota exceeded
console.error('Quota exceeded')
} else {
// rethrow other exceptions
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 |