# Web Storage: localStorage, cookies & IndexedDB

> Learn how to save data directly inside the user's browser. Compare the persistence, capacity, and use-cases of localStorage, sessionStorage, Cookies, and IndexedDB.

Subject: JavaScript | Lesson 36 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/web-storage
Previous: [Fetch, AJAX & HTTP](https://codewithanimation.in/javascript/fetch-and-ajax.md) | Next: [Timers & requestAnimationFrame](https://codewithanimation.in/javascript/timers-and-raf.md)

## Client-Side Databases

Historically, the only way to remember a user across page reloads was by storing data on a remote server.

Modern browsers now feature built-in storage systems, allowing your JavaScript to save user preferences, shopping carts, or even entire offline applications directly on their device.

## The Four Storage Types

1. `localStorage`: Survives closing the browser. Stores about 5MB. Synchronous.

2. `sessionStorage`: Survives page reloads, but dies when the tab is closed. Stores about 5MB. Synchronous.

3. `Cookies`: Tiny (4KB) and sent to the server with *every* HTTP request. Used for authentication.

4. `IndexedDB`: A massive, asynchronous NoSQL database that can store hundreds of megabytes of complex objects.

## Setting and Getting

The Web Storage API (`localStorage` and `sessionStorage`) is extremely simple. It acts like a giant dictionary.

`localStorage.setItem('theme', 'dark')` saves a value.

`const theme = localStorage.getItem('theme')` reads it back.

## Common mistake: The String Constraint

The biggest limitation of `localStorage` is that it can **only store strings**.

If you try to save a JavaScript object or array directly (`localStorage.setItem('user', { name: 'Link' })`), it gets converted into the useless string `[object Object]`.

You must use `JSON.stringify(data)` when saving, and `JSON.parse(data)` when reading.

## Tip: Private Browsing

When a user is in Incognito or Private Browsing mode, `localStorage` still works, but it acts like `sessionStorage` - everything is wiped clean the moment they close the private window.

## Try it: Persistence in Action

Change the theme preference and watch it save to localStorage. Then, simulate a page reload to see how the browser instantly remembers your choice!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/web-storage

## Check yourself

### Question 1

If you want to save a user's shopping cart so it is still there when they return to your website tomorrow, which should you use?

- A. localStorage
- B. sessionStorage
- C. Cookies

Correct answer: A

`localStorage` persists data indefinitely until explicitly cleared, making it perfect for saving non-sensitive data across multiple sessions.

- Why not B: Incorrect. `sessionStorage` is destroyed when the tab is closed.
- Why not C: Incorrect. A shopping cart is too large for the 4KB limit of a cookie.

### Question 2

How do you correctly save a JavaScript array in `localStorage`?

- A. localStorage.setItem('list', JSON.stringify([1, 2, 3]))
- B. localStorage.setItem('list', [1, 2, 3])
- C. localStorage.setItem('list', [1, 2, 3].toString())

Correct answer: A

Because `localStorage` only accepts strings, you must serialize arrays and objects into JSON strings before saving them.

- Why not B: Incorrect. `localStorage` coerces it to a string, resulting in the literal string '1,2,3' which loses array features.
- Why not C: Incorrect. While this works, parsing it back into an array later is extremely difficult compared to JSON.

### Question 3

Which storage mechanism is designed specifically to be sent back and forth between the browser and the server?

- A. Cookies
- B. localStorage
- C. IndexedDB

Correct answer: A

Cookies are unique because they are automatically attached to the HTTP headers of every request sent to the server, making them ideal for authentication tokens.

- Why not B: Incorrect. Web Storage never leaves the browser.
- Why not C: Incorrect. IndexedDB is strictly client-side.

## Remember this

- `localStorage` persists data even after the browser is closed.
- `sessionStorage` clears data when the tab is closed.
- Web Storage can only store strings; use `JSON.stringify` for objects.
- Cookies are tiny (4KB) and sent to the server automatically.
- IndexedDB is an asynchronous database for massive amounts of data.
