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
HTML Frames
HTML Iframes
HTML5 Introduction
HTML Multimedia
HTML5 Video
HTML5 Audio
HTML5 Canvas
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What are the new multimedia elements in HTML5?
How do you insert a video in HTML?
What is the difference between HTML and HTML5?
What is an iframe in HTML and what is it used for?
What is the difference between canvas and SVG in HTML5?
What is the difference between localStorage, sessionStorage and cookies?