Awonke Mnotoza

Site Owner

Local Storage with JavaScript

07 May 2023

0
share

What is Local Storage?

This is a feature found in modern browsers that allows web applications to store data locally within the user's browser.

Meaning that the data will always be there, even if the user closes the browser or navigates to a different website.

Why would I need to use Local Storage?

Local Storage is typically used for storing small amounts of data, such as when a user is filling in a form and refreshes the browser by mistake, you want to keep that data so that the user must not fill it in again. Then remove the data when the form is submitted.

You do not want to store huge data like users to do items if you are creating a to-do application, databases are created for that.

Another use case for Local Storage would be the changing of the theme. It is pointless to use a database for that.

Now, how do I use this Local Storage?

Storing the data

Contawo: javascript

Copy

localStorage.setItem('itemKey', 'itemValue')

The setItem will allow you to set the item on local storage.

It takes in two arguments, the itemKey is used to identify the data in the local storage. You will use this key to get the data from the Local Storage.

The itemValue is the actual data that you want to be stored on the Local Storage.

There are some cases where you will want to store an object or an array or any data type but Local Storage supports strings as the itemValue, therefore you cannot store the object or array directly.

Here is how you would want to store them.

Contawo: javascript

Copy

localStorage.setItem('itemKey', JSON.stringify(dataType)

JSON.stringify will convert that data type into a string

Reminder, the setItem takes strings as arguments not anything.

Retrieving the data from Local Storage:

Contawo: javascript

Copy

localStorage.getItem('itemKey')

The getItem will allow you to get the item that you stored.

It takes in one argument which is the item key that you want to retrieve.

The itemKey must be the same as the one that you used when storing the data.

It will return the data you stored as a string. What if the data you stored was not a string, how do you convert the string to the data that you stored?

Contawo: javascript

Copy

let data = localStorage.getItem('itemKey');
let convertData = JSON.parse(data)

JSON.parse will convert your data into the original format that it was in when you stored the data.

Something to note, the amount of data you can store in Local Storage is typically around 5-10 MB depending on the browser.

Item Keys in Local Storage:

Local Storage can be also used to store data that is specific to a particular website or domain.

This is achieved by setting unique keys for each item stored in the Local Storage.

You do not want to store a list store a list of to-do's and get a list of chores because of a weak key "list".

Set a unique key. Be creative. Be a developer.

Remember to handle error scenarios and provide appropriate feedback to the user. Additionally, you may want to consider implementing some form of data validation to ensure the integrity of the stored data.

Learn more about JavaScript and the Web

If you are curious like me and want to learn more about JavaScript and the Web.

Also get a certificate of completion, so that you can start applying for jobs.

Start learning today by clicking the link below.

Conclusion

Local Storage is a useful feature that allows web developers to store user data locally.

However, it is important to note that Local Storage has some limitations.

Local Storage is not secure, it can be accessed by anyone who has access to the user's device. So you do not want to store user-sensitive data.

Please use Local Storage properly.

Please comment on your thoughts and any questions you have.

Comments

Be the first to comment...