DEV Community

Jatin Gupta
Jatin Gupta

Posted on

Cookies vs Local Storage: What’s the Real Difference?

“Both store data in the browser, so what’s the big difference?”

But once you start working with authentication, APIs, sessions, security, or even interviews, the difference becomes much more important.

And there’s one thing I’ve noticed people often get wrong:

Cookies are not simply “more secure” than Local Storage.

Security depends on what you store, how you store it, how your authentication works, and how you protect against attacks like XSS and CSRF.

So let’s break this down properly.

What Are Cookies?

Cookies are small pieces of data stored by the browser and associated with a website.

One of their biggest differences from Local Storage is that cookies can be automatically included in HTTP requests to the appropriate server.

For example, imagine you log into:

https://example.com
Enter fullscreen mode Exit fullscreen mode

The server creates a session and sends a cookie:

Set-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Lax
Enter fullscreen mode Exit fullscreen mode

Your browser stores it.

Later, when you make a request to the same site, the browser can automatically send:

Cookie: sessionId=abc123
Enter fullscreen mode Exit fullscreen mode

You don't have to manually add it to every request.

That's one reason cookies are commonly used for session-based authentication.

What Is Local Storage?

Local Storage is a browser-based storage mechanism that allows JavaScript to store data as key-value pairs.

For example:

localStorage.setItem("theme", "dark");
Enter fullscreen mode Exit fullscreen mode

And later:

const theme = localStorage.getItem("theme");
Enter fullscreen mode Exit fullscreen mode

The important part is:

Local Storage does not automatically send its contents to your server.

If your frontend makes this request:

fetch("/api/profile");
Enter fullscreen mode Exit fullscreen mode

the browser doesn't automatically attach:

localStorage data
Enter fullscreen mode Exit fullscreen mode

to the request.

If you want to send something from Local Storage, your JavaScript code has to explicitly add it.

For example:

const token = localStorage.getItem("token");

fetch("/api/profile", {
  headers: {
    Authorization: `Bearer ${token}`
  }
});
Enter fullscreen mode Exit fullscreen mode

This difference is fundamental.

The Simplest Way to Remember It

I like to remember it this way:

🍪 Cookie

Browser  <──────────>  Server
             HTTP


💾 Local Storage

Browser  <──────────>  Browser
          JavaScript
Enter fullscreen mode Exit fullscreen mode

Cookies are designed to participate in web requests.

Local Storage is primarily a client-side storage mechanism.

What Does HttpOnly Actually Do?
This is one of the most important cookie attributes to understand.

Consider:

Set-Cookie: sessionId=abc123; HttpOnly
Enter fullscreen mode Exit fullscreen mode

HttpOnly means that JavaScript cannot access that cookie through APIs such as:

document.cookie
Enter fullscreen mode Exit fullscreen mode

So if you have:

sessionId=abc123
Enter fullscreen mode Exit fullscreen mode

JavaScript cannot simply do:

console.log(document.cookie);
Enter fullscreen mode Exit fullscreen mode

and retrieve that HttpOnly cookie.

But the browser can still send it with appropriate requests.

This creates an important security boundary:

JavaScript ❌ → HttpOnly Cookie

Browser ✅ → Server
Enter fullscreen mode Exit fullscreen mode

This can significantly reduce the impact of certain XSS scenarios involving stealing authentication cookies.

But remember:

HttpOnly does not prevent XSS itself.

If an attacker manages to execute JavaScript in your application, they may still be able to perform actions as the victim through the browser, even if they cannot directly read the cookie.

🔒 What Does Secure Mean?
Another commonly misunderstood attribute is:

Secure
Enter fullscreen mode Exit fullscreen mode

For example:

Set-Cookie: sessionId=abc123; Secure
Enter fullscreen mode Exit fullscreen mode

A Secure cookie is only sent over HTTPS connections, apart from certain localhost development considerations defined by browsers.

So:

HTTP  ❌
HTTPS ✅
Enter fullscreen mode Exit fullscreen mode

If you're dealing with authentication cookies, using HTTPS and the Secure attribute is extremely important.

🛡️ What Does SameSite Do?

Now we get to another important security concept:

SameSite
Enter fullscreen mode Exit fullscreen mode

It controls when cookies are included in requests involving different sites.

Common values include:

SameSite=Strict
SameSite=Lax
SameSite=None
Enter fullscreen mode Exit fullscreen mode

For example:

Set-Cookie: sessionId=abc123; Secure; HttpOnly; SameSite=Lax
Enter fullscreen mode Exit fullscreen mode

SameSite is particularly relevant when thinking about CSRF (Cross-Site Request Forgery).

The basic idea behind CSRF is:

An attacker tries to make a user's browser send an authenticated request to your application without the user intentionally making that request.

Because cookies can be automatically attached to requests, authentication cookies need appropriate CSRF considerations.

SameSite can help reduce this risk, but it isn't a replacement for understanding your application's complete security model.

Top comments (0)