JavaScriptBeginner#browser#storage

What are localStorage, sessionStorage and cookies?

localStorage persists until cleared, sessionStorage clears when the tab closes; both hold about 5 MB of strings and are never sent to the server. Cookies are small, can expire and are attached to every matching request.

Example
localStorage.setItem('theme', 'dark');
JSON.parse(localStorage.getItem('user') ?? 'null');
sessionStorage.setItem('step', '2');

Related Questions

1
JavaScriptIntermediate#browser#async

How does fetch differ from XMLHttpRequest?

Open
2
JavaScriptIntermediate#browser#security

What is CORS?

Open
3
JavaScriptBeginner#async

What is the difference between synchronous and asynchronous code?

Open