Valerie M.
—You’d like to save arrays and objects to browser storage using JavaScript as an alternative to using cookies. Is there a way to do this?
There are a couple of ways to save arrays and objects to browser storage.
The Web Storage API allows us to store items in a more intuitive way using two mechanisms: localStorage
and sessionStorage
.
For both of these mechanisms:
JSON.stringify()
.JSON.parse()
.Let’s look at some code examples.
localStorage
APIThe localStorage
API maintains browser storage even when the browser is closed and reopened.
The code example below shows how the localStorage
API is used to store arrays:
const myArray = ['a', 'b', 'c', 'd']; // convert array to JSON string using JSON.stringify() const jsonArray = JSON.stringify(myArray); // save to localStorage using "array" as the key and jsonArray as the value localStorage.setItem('array', jsonArray); // get the JSON string from localStorage const str = localStorage.getItem('array'); // convert JSON string to relevant object const parsedArray = JSON.parse(str); console.log(parsedArray);
To save objects, replace myArray
with something like myObject
. We’ll use the sessionStorage
API to demonstrate saving objects for brevity.
sessionStorage
APIThe sessionStorage
API stores items for as long as the browser window is open, including page reloads and restores. The data is lost once the browser window is closed.
Here’s a code example to show how to use the sessionStorage
API to store objects:
const Person = { name: 'John Smith', age: 18, }; // convert object to JSON string using JSON.stringify() const jsonObject = JSON.stringify(Person); // save to localStorage sessionStorage.setItem('person', jsonObject); // get the JSON string from sessionStorage const str = sessionStorage.getItem('person'); // convert JSON string to valid object const parsedObject = JSON.parse(str); console.log(parsedObject);
To remove items from local and session storage, use the removeItem()
method, giving it the key, as follows:
localStorage.removeItem('array'); sessionStorage.removeItem('person');
The local and session storage can also be cleared using clear()
:
localStorage.clear(); sessionStorage.clear();
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.