


We use cookies to improve your experience
We use essential cookies to make our site work. With your consent, we may also use non-essential cookies to improve user experience.
Definition
LocalStorage is a web browser API that allows websites to store key-value pairs persistently on the user's device. Unlike cookies, LocalStorage data is not sent to the server with every request, has a larger storage limit (typically 5-10MB), and does not expire automatically.
Cookies and LocalStorage both store data on the client, but they serve different purposes. Cookies are automatically included in HTTP requests (useful for authentication), limited to about 4KB, and can be configured to expire. LocalStorage is only accessible via JavaScript, supports 5-10MB of storage, persists until explicitly cleared, and is never sent to the server automatically. For client-side data that does not need to reach the server (user preferences, cached UI state, draft form data), LocalStorage is the better choice.
The API is intentionally simple: localStorage.setItem("key", "value") stores data, localStorage.getItem("key") retrieves it, and localStorage.removeItem("key") deletes it. Values must be strings — objects and arrays need JSON.stringify() before storage and JSON.parse() when retrieved. LocalStorage is synchronous and blocks the main thread, so storing or retrieving large amounts of data can cause performance issues.
LocalStorage has meaningful limitations: 5-10MB cap, synchronous-only access, string-only values, and no indexing or querying capability. For larger or more complex client-side storage needs, IndexedDB offers asynchronous access, structured data, indexes, and much larger quotas (hundreds of MB or more). SessionStorage works identically to LocalStorage but is scoped to a single browser tab and cleared when the tab closes.