Cookies vs. LocalStorage

Cookies and localstorage are both ways for websites to store small amounts of data in your browser, but they serve very different purposes and behave quite differently.

Here’s a clear comparison:

FeatureCookiesLocalStorage
PurposeMainly for server-client communication (sessions, tracking, preferences)Client-side only data storage (user preferences, saved projects, settings)
Sent with requestsYes — automatically sent with every HTTP request to the same domainNo — never sent to the server
Storage limitAbout 4 KB per cookieAbout 5–10 MB per origin (much larger)
ExpirationCan expire (session cookies or set expiration date)Permanent until manually deleted
AccessibilityCan be set to HttpOnly (inaccessible to JavaScript)Always accessible via JavaScript
SecurityCan be more secure with HttpOnly and Secure flagsMore exposed to XSS attacks
Data typeOnly stringsStrings (but you can JSON.stringify objects)
Use case exampleLogin session, CSRF tokens, analytics trackingSaving woodworking project dimensions, user theme preference, or a list of favorite tools

Quick woodworking-related examples

  • Cookie: A site might use a cookie to remember you’re logged into the WoodCentral forum so you don’t have to log in every time you visit.
  • LocalStorage: The site could save your preferred board-foot calculator settings or the last few lumber dimensions you entered so they’re still there when you come back next week.

Rule of thumb:

  • Use cookies when the server needs to know the information.
  • Use localStorage when only the browser needs to remember something and you want more storage space.

Leave a Comment

Licensed under CC BY-NC 4.0

Peter Martin viewpoints are those of its owner. You may share and adapt this article for non-commercial purposes, provided proper attribution is given. Attribution should include:

Title: Cookies vs. LocalStorage
Author: Peter Martin
Original URL: https://www.woodcentral.com/-/peter/cookies-vs-localstorage/
License: CC BY-NC 4.0

Site Index