Window.localStorage
localStorage
is part of Web Storage API
, that allows you to store some data without expiration like sessionStorage
. This is feature of almost all modern browsers, that allows you to store key/value pairs for the purpose of reusing them.
It is meant to be kind of replacement for some usage of cookies
localStorage.getItem()
and localStorage.setItem()
are most common methods used, to retrieve data for given key, and set data value for given key.
Typical use for localStorage.setItem()
is when you need to store some data for future use, so it won't be lost when user refreshes page, or opens other page.
When you need to retrieve data you stored, you use localStorage.getItem()
.
Similar to localStorage
, you have sessionStorage
, that is similar. Only difference is that sessionStorage
has expiration time, so it is best to use it when you don't want to temporary store some data.
Important:
Keep in mind that storing data in this way is not secure, so avoid storing confidential data.
JSON.parse
and JSON.stringify
JSON.parse
is used to convert JSON string format into an object, and JSON.stringify
is used to convert object into string.
Typical use for JSON.parse
is when you get string from some external source, since string is the way to store the data. It converts string into an object, which can be used in your JavaScript code to manipulate with data properties from that object.
JSON.stringify
is mostly used to store data as a string, after you did some manipulation with properties from an object.
See more about Web Storage API:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
About JSON and JSON.stringify
and JSON.parse
methods:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON