What is the difference between cookies, local storage, and session storage?

JavaScript, Browser, Web development · Jun 12, 2021

Cookies store small amounts of data that has to be sent back to the server with subsequent requests and their expiration can be set from either server or client. They are primarily used for server-side reading.

  • Capacity: 4KB
  • Accessible from: Any window
  • Expiration: Manually set
  • Storage location: Browser and server
  • Sent with requests: Yes
  • Blockable by users: Yes
  • Editable by users: Yes

Local storage stores a larger amount of data on the client's computer in a key-value pair format and has no expiration date. Data is never transferred to the server and is accessible via JavaScript and HTML5.

  • Capacity: 10MB
  • Accessible from: Any window
  • Expiration: Never
  • Storage location: Browser only
  • Sent with requests: No
  • Blockable by users: Yes
  • Editable by users: Yes

Session storage stores a larger amount of data on the client's computer only for the current session, expiring the data on tab close. Data is never transferred to the server and is accessible client-side from the same tab.

  • Capacity: 5MB
  • Accessible from: Same tab
  • Expiration: On tab close
  • Storage location: Browser only
  • Sent with requests: No
  • Blockable by users: Yes
  • Editable by users: Yes
CookiesLocal storageSession storage
Capacity4KB10MB5MB
Accessible fromAny windowAny windowSame tab
ExpirationManually setNeverOn tab close
Storage locationBrowser and serverBrowser onlyBrowser only
Sent with requestsYesNoNo
Blockable by usersYesYesYes
Editable by usersYesYesYes

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub.

More like this