Using LocalStorage to Persist Data
Tags: javascript, html, beginners
Published: 2020-05-29 01:15:34

Making an app that requires persisting some simple but non-sensitive data? Do you want to cache some data to help your app run faster when a user returns? Using web storage can make that extremely simple if the data being stored doesn't need security.

There are two types of web storage objects:
 - localStorage (no specified expiration and will persist after the window closes)
 - sessionStorage (cleared when the browser window is closed)

I will only be diving into localStorage for this blog. <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage">More info on how web storage data is stored as well as browser compatibility.</a>

<h3>Note on Security</h3>

While web storage can be very convenient, never store sensitive user information using web storage. No data protection measures are in place for localStorage. Keep in mind that localStorage is compatible with most browsers, but not all, so make sure your app doesn’t rely on localStorage to run. <a href="https://dev.to/rdegges/please-stop-using-local-storage-1i04">Here is another blog post going more in-depth about why and when using localStorage can be dangerous.</a>

<h2>What is localStorage exactly?</h2>

The localStorage is just a JS object. If you open up dev tools and type localStorage into the console, you should get back a JS object. If the length is greater than 0, there is information stored there by the page you are currently on. When I looked at localStorage for dev.to, there is actually a ‘current_user’ key that has a lot of my info saved under it like id, username, profile_image, etc. You can visit any webpage you like and see if and how that page uses localStorage.

![local storage object in dev tools](https://dev-to-uploads.s3.amazonaws.com/i/rkq7e5r04c7njez97u08.png)

The localStorage object has a few methods we can make use of:
 1. localStorage.clear()
 2. localStorage.setItem(key, value)
 3. localStoage.getItem(key)
 4. localStorage.removeItem(key)
 5. localStorage.key(index)

Note: sessionStorage has the exact same methods.

<h3>.clear()</h3>

To start playing around with localStorage in the browser, we can start with a clean slate by running localStorage.clear().

![clearing localStorage](https://dev-to-uploads.s3.amazonaws.com/i/kuc5kigrzp09p1of13er.png)

This removes all items saved to localStorage at this domain.

<h3>.setItem()</h3>

Now let's add an key-value pair to localStorage:

![using localStorage.setItem() method](https://dev-to-uploads.s3.amazonaws.com/i/qext13n9ysf93mdr4vky.png)

In this example, we are going to be storing a simple grocery list.

<h3>.getItem()</h3>

Let's see if our data is saved properly by using the getItem method.

![using localStorage.getItem() method](https://dev-to-uploads.s3.amazonaws.com/i/bsp2spmagm6dtqc3xgoq.png)

Our data was saved, but the array was converted to a string in a way we didn't intend. To fix this, we can use JSON.stringify() and JSON.parse() in order to convert our array into a string for localStorage and back to an array once we retrieve it.

![using JSON.stringify() to convert our array into a string and save it to localStorage](https://dev-to-uploads.s3.amazonaws.com/i/z4sov8lwqrwz3ta9kp41.png)

<h3>.removeItem()</h3>

To remove our key-value pair, we just need to use the removeItem method.

![using localStorage.removeItem() method](https://dev-to-uploads.s3.amazonaws.com/i/m3ia8reas1j7taynl9kj.png)

<h3>.key()</h3>

The only other method available to us it localStorage.key(index). This method returns the key of an item when passed an index. This could be useful if you had to loop through the data you stored in localStorage and pass in an index.

<h2>Play Around!</h2>

Since localStorage is easy to access in dev tools, I would highly recommend going to some of your favorite sites and seeing if/how they use localStorage and sessionStorage. This may help you get ideas on when you might use it and get insight into the security of different websites.