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 |
| No Content Found. |
| Exercises & Assignments |
| No Content Found. |
| Interview Questions & Answers |
| No Content Found. |