What is the difference between localStorage, sessionStorage and cookies?

All three store data in the visitor’s browser, but they work differently.

PointlocalStoragesessionStorageCookies
LifetimeUntil deletedUntil the tab is closedUntil the expiry date
SizeAbout 5 MBAbout 5 MBAbout 4 KB
Sent to server with every requestNoNoYes
Use forSettings, theme, saved draftsData of one tab sessionLogin sessions, tracking
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme"));   // dark

sessionStorage.setItem("step", "2");

Never store passwords or other secrets in web storage, because any JavaScript on the page can read it. Web storage is a part of HTML5.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.