This content originally appeared on DEV Community and was authored by Stephen O.
A buddy of mine asked me, “Aside database, are there other methods one can store data from the web”. I gave him a response of course, and thought to render same knowlege to others hungry for such insight. So, here’s one for all of you.
When you’re starting out in web development, you’ll encounter three important ways to store data in the browser: Local storage, Session storage, and Cookies. These methods help websites remember information about you or your activity, creating a more personalized and seamless browsing experience.
Although they are all storage methods, each serves distinct purposes and has unique characteristics.
Cookies: The Web’s Original Memory System
Cookies are the oldest and most widely recognized method of storing data in web browsers. Think of them as small digital notes that websites leave on your device. Each cookie is tiny, limited to about 4KB of data, which is roughly equivalent to a few paragraphs of text. What makes cookies special is that they’re automatically sent back to the server with every request you make to the website. This two-way communication allows servers to maintain your login session or remember your preferences across different pages.
Cookies come with expiration dates. Some might last just until you close your browser (session cookies), while others can persist for months or even years (persistent cookies). We software developers often use cookies for authentication, remembering login sessions, tracking user behavior, and personalizing content. For example, when a shopping site remembers items you’ve viewed or keeps you logged in between visits, it’s typically using cookies to accomplish this.
Here’s a simple example of how cookies work in code:
// Setting a cookie that expires in one year
document.cookie = "user_theme=dark; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";
// Reading all cookies
console.log(document.cookie); // Outputs something like "user_theme=dark; session_id=abc123"
Session Storage: Temporary Memory for Single Browsing Sessions
Session storage provides a more substantial storage solution (typically 5-10MB) that exists only for the duration of a single browser tab. Imagine it as a notepad that’s available only while you’re actively working in that specific tab – once you close the tab, all the notes disappear.
This temporary nature makes session storage ideal for information that’s only relevant during your current visit to a website.
Unlike cookies, session storage data isn’t automatically sent to the server, and it’s isolated to the specific tab where it was created. This means if you open the same website in another tab, it won’t have access to the session storage from your first tab. Developers commonly use session storage for temporarily saving form data, preserving application state, or storing other information that would be useful during the current session but doesn’t need to persist long-term.
Working with session storage is straightforward:
// Save data to sessionStorage
sessionStorage.setItem('unsaved_form_data', JSON.stringify(formInputs));
// Retrieve data later
const savedData = sessionStorage.getItem('unsaved_form_data');
// Clear specific data when no longer needed
sessionStorage.removeItem('unsaved_form_data');
Local Storage: Persistent Client-Side Storage
Local storage offers a more permanent solution for storing data in the browser. With a similar capacity to session storage (typically 5-10MB), local storage persists even when you close your browser, restart your computer, or return to the site days later.
You can think of it as a dedicated filing cabinet that the website can use on your device to store information indefinitely.
This persistence makes local storage perfect for remembering user preferences like theme choices, saving application state between visits, or caching data to improve performance.
Unlike cookies, local storage data isn’t automatically sent to the server, which can be beneficial for reducing unnecessary network traffic. However, it’s important to remember that users can clear this storage at any time, so critical data should still be saved server-side when needed.
Here’s how you might use local storage in practice:
// Store user preferences
localStorage.setItem('preferred_language', 'en-US');
// Retrieve the stored value
const languagePreference = localStorage.getItem('preferred_language');
// Remove a stored item
localStorage.removeItem('preferred_language');
Final Thoughts
While these storage options seem similar, they serve different purposes:
- Cookies are the old reliable method that works everywhere but are limited
- Session storage is great for temporary, tab-specific data
- Local storage is your go-to for persistent client-side data
As a developer not conversant with storage logics, start experimenting with these in small projects. You’ll quickly get a feel for when each one is most appropriate!
This content originally appeared on DEV Community and was authored by Stephen O.