<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/localstorage-vs-sessionstorage-vs-cookies-hkcgxgftt" -->

---
title: LocalStorage vs SessionStorage vs Cookies | daily.dev
description: A beginner-focused guide comparing LocalStorage, SessionStorage, and Cookies as browser data storage mechanisms. Covers persistence duration, scope (origin vs...
canonical: https://daily.dev/posts/localstorage-vs-sessionstorage-vs-cookies-hkcgxgftt
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: LocalStorage vs SessionStorage vs Cookies | daily.dev
og:description: A beginner-focused guide comparing LocalStorage, SessionStorage, and Cookies as browser data storage mechanisms. Covers persistence duration, scope (origin vs...
og:url: https://daily.dev/posts/localstorage-vs-sessionstorage-vs-cookies-hkcgxgftt
og:image: https://api.daily.dev/og/posts/hKCGxGftt.png
og:image:alt: LocalStorage vs SessionStorage vs Cookies
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# LocalStorage vs SessionStorage vs Cookies

**[Anthony Max](https://daily.dev/sources/lfrkmbqjvju9ag1huqtvg)** · [@aanthonymax](https://daily.dev/aanthonymax) · 3 min read · 1 upvotes · 0 comments

## Summary

A beginner-focused guide comparing LocalStorage, SessionStorage, and Cookies as browser data storage mechanisms. Covers persistence duration, scope (origin vs tab), storage size limits, and typical use cases like caching settings, session tokens, and tracking. Includes basic JavaScript code snippets for setting, getting, and clearing data in each storage type.

## Content

When we talk about such a topic as data storage, we immediately remember these 3 concepts. But here's the problem, we often use them unconsciously.

We are used to storing tokens in sessionStorage, but not everyone can answer why exactly. Other concepts follow the same scenario. All these issues have long been decided for us by the modules that we use, and this is sad, because you need to know about this, even if you do not understand websites at all.

In this article, I tried to prepare for you the ultimate guide so that you do not have any misunderstandings about what is used and for what.

## 🗄️ LocalStorage

Before we start with LocalStorage, it's worth taking a short detour and talking about what storage is.

Simply put, **storage is** a dedicated place where the browser stores information. In JavaScript, such storages are represented as objects available via the `window` object’s properties with reserved names, or through dedicated variables.

Now, let's go back. There is a LocalStorage storage, which we access through the `localStorage` object. In this object itself, what is important now, the data is stored even when we close the tab or the browser itself. Suitable for large amounts of data.

```
// Save a value
localStorage.setItem("theme", "dark");

// Retrieve a value
const theme = localStorage.getItem("theme");
console.log(theme); // "dark"

// Remove a value
localStorage.removeItem("theme");

// Clear all storage
localStorage.clear();
```

It's a good place to store long-lived data, such as settings, cached data, etc.

---

## 🕓 SessionStorage

SessionStorage is similar to LocalStorage; the difference is that while `localStorage` is partitioned by origin only, `sessionStorage` is partitioned by both origin and browser tabs (top-level browsing contexts). The data in `sessionStorage` is only kept for the duration of the page session. Suitable for large amounts of data.

That is, if you close a tab in your browser, all your data in this storage will be cleared.

```
// Save a value
sessionStorage.setItem("authToken", "123456");

// Retrieve a value
const token = sessionStorage.getItem("authToken");
console.log(token); // "123456"

// auth

// Remove a value
sessionStorage.removeItem("authToken");

// Clear all storage
sessionStorage.clear();
```

Suitable for temporary tokens, as well as for data that is needed within the framework of while you are sitting at the site, that is, session data.

---

## 🍪 Cookies

And now, probably the most popular storage, which is on everyone's lips with modern web application users, is cookies. The banner with them is constantly displayed, but many miss the point: "what are they for?" In fact, this is a small storage (in size later), which stores temporary data (that is, data that will be deleted after a certain period). Suitable for small amounts of data.

```
// Set a cookie
document.cookie = "username=John; path=/; max-age=3600"; // expires in 1 hour

// Read cookies
console.log(document.cookie); // "username=John"

// Update a cookie
document.cookie = "username=Mike; path=/; max-age=3600";

// Delete a cookie
document.cookie = "username=; path=/; max-age=0";
```

This data is often used in all sorts of tracking on websites. How many people clicked, how someone moves the cursor, etc. Also, when working with authentication, this storage is often used.

---

## ✅ Conclusion

In this article, we looked at LocalStorage, SessionStorage, and Cookies, which perform different tasks for storing data in the browser. Choosing the right storage method depends on the lifetime of the data, the volume limitation, and the need to access it from the server. The right choice will allow you to improve performance, security, and the quality of the user experience.

## Similar posts on daily.dev

- [Explore Chrome Incognito Mode from a storage & cookie & caching perspective](https://daily.dev/posts/explore-chrome-incognito-mode-from-a-storage-cookie-caching-perspective-oizcxz63k) · Medium · 1 upvotes · 0 comments
- [What's the best way to do authentication in modern applications](https://daily.dev/posts/what-s-the-best-way-to-do-authentication-in-modern-applications-7iho9es5q) · Neciu Dan · 25 upvotes · 1 comments
- [Auth Mastery Part 2: Sessions, Cookies, and Staying Authenticated](https://daily.dev/posts/auth-mastery-part-2-sessions-cookies-and-staying-authenticated-zk5kkxcot) · InfoSec Write-ups · 0 upvotes · 0 comments

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#javascript](https://daily.dev/tags/javascript)

[View this post on daily.dev](https://daily.dev/posts/localstorage-vs-sessionstorage-vs-cookies-hkcgxgftt)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/localstorage-vs-sessionstorage-vs-cookies-hkcgxgftt","headline":"LocalStorage vs SessionStorage vs Cookies","text":"A beginner-focused guide comparing LocalStorage, SessionStorage, and Cookies as browser data storage mechanisms. Covers persistence duration, scope (origin vs tab), storage size limits, and typical use cases like caching settings, session tokens, and tracking. Includes basic JavaScript code snippets for setting, getting, and clearing data in each storage type.","url":"https://daily.dev/posts/localstorage-vs-sessionstorage-vs-cookies-hkcgxgftt","datePublished":"2026-10-04T10:50:28.686Z","dateModified":"2026-10-04T10:52:34.644Z","author":{"@type":"Person","name":"Anthony Max","url":"https://daily.dev/aanthonymax","image":"https://media.daily.dev/image/upload/s--IOCG5ocV--/f_auto/v1745861623/avatars/avatar_lfRKMBQjVJU9aG1HuqtVg","description":"Fetch API enjoyer","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":1320}},"image":"https://media.daily.dev/image/upload/s--E6t4jO9r--/f_auto/v1791111028/posts/hKCGxGftt?_a=BAMAMicg0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/sources/lfrkmbqjvju9ag1huqtvg","name":"Anthony Max"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Anthony Max","item":"https://daily.dev/sources/lfrkmbqjvju9ag1huqtvg"},{"@type":"ListItem","position":3,"name":"LocalStorage vs SessionStorage vs Cookies"}]}
```

