Site Owner
07 May 2023
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.
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.
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
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.
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.
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.
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.
Contact me to advertise with Contawo.
Comments
Be the first to comment...