February 05, 2020
There are three ways of storing sessions in your browser.
In this article, I’ll try to explain their differences so next time we’re asked this question, we can reason better and choose the most useful of the options.
Local Storage means using localStorage
to store your key-value pair in the
browser. When using this way of storing, you don’t lose the data even when:
The only way a data can be removed from the local storage is either using
localStorage.removeItem
to remove a specific key-value pair or clearing
the browser memory.
This type of storing is a bit far from what localStorage
is.
Because of this limitation, sessionStorage
is used less compared to localStorage
.
Amongst the three options, I’d say cookies is the most efficient way of
storing sessions. It has the same similarities with localStorage
but the special
thing about cookies is that it is automatically sent on every HTTP request —
which only a web server can access. It allows us to store data better and most
especially for complex cases like a multi-step registration form where each step
goes to the web server.