What are the differences, pros and cons between localStorage
and sessionStorage
?
What are the differences, pros and cons between localStorage
and sessionStorage
?
Both localStorage
and sessionStorage
range from Storage
. There is no difference between them except for the non-persistence of sessionStorage
.
Non-persistence as described above is in the sense that sessionStorage
is only available for the window that created the data until such window is closed, when opening another window > or tab ) such data will not be available.
In contrast to sessionStorage
, when creating data in localStorage
this data will be available to any tab / window even if the user exits the window, restart the system, etc.
As an example, let's assume that you want to save the user name and password to log in, you may choose to store this data in sessionStorage
for security reasons and save user settings to localStorage
.
Use localStorage
for long-term use and sessionStorage
when you need to store something temporary (ie session data ).
Both are also scoped by browser makers. So the storage data saved by IE can not be read by Chrome or FF .
You can see a demonstration here .
sessionStorage
- Similar to a cookie
that expires when you close the browser. it has the same structure as localStorage
, but there is no way to change its permanence time, whenever close will be deleted.
localStorage
- Similar to a cookie
", but it never expires, so while there were records in the AppCache
for that domain, the variable will exist (Unless the user creates a routine to remove it).
Both attributes have been added by HTML5 . The above items called web storage.
But there are other forms of local storage through HTML5
, such as database WebSQL
, Database indexed data (IndexDB)
as well as conventional files (File Access)
.