javascript – When to use cookies and when to use localstorage?


Cookie and localstorage – does the second completely replace the first, or does it all depend on the situation? If it depends on the situation, how do you know when to use one and when to use the other?

Colleagues, thank you for your comprehensive answer!


Cookies are sent to the server on every request – so it makes sense to use them for something that is needed on the server. Let's say, authorization, or, for example, view parameters needed when building a page on the server, such as language.

Everything else that is needed only at the front-end level, as I understand it, is better kept in Local Storage. More will fit there, and there will be no overflow problems (if there are too many cookies, some servers may stop issuing any response at all).

If you need support for absolutely all browsers like IE7, I would advise you to use a polyfill, so that in this case, cookies are used only as a last resort.

Scroll to Top