What is the difference between localStorage, sessionStorage and cookies?
All three store data in the visitor’s browser, but they work differently.
| Point | localStorage | sessionStorage | Cookies |
|---|---|---|---|
| Lifetime | Until deleted | Until the tab is closed | Until the expiry date |
| Size | About 5 MB | About 5 MB | About 4 KB |
| Sent to server with every request | No | No | Yes |
| Use for | Settings, theme, saved drafts | Data of one tab session | Login 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. |