LocalStorage In Javascript

Darshan Dixit
Catalysts Reachout
Published in
4 min readSep 28, 2022

We usually think that storing data is possible only by communicating with a backend server. But these methods are quite old as nowadays the data can be stored in browsers and applications without using servers. Through LocalStorage we can store data in both browsers as well as applications.

Let’s talk about Web Storage

Let’s look at the definition of online storage before getting down to business. One of HTML5’s wonderful features is web storage, which enables online applications to store data locally in a user’s browser.

LocalStorage and Session Storage are the two most used types of online storage. Data saved in LocalStorage never leaves the browser and stays there unless you specifically remove it, which is the fundamental distinction between these two types of web storage. When a browser tab or window is closed, however, Session Storage data is deleted.

What is LocalStorage?

As was already noted, LocalStorage is a type of web storage made available by the browser that enables web applications to store data locally and indefinitely within a user’s browser. When a browser window or tab is closed in this case, the stored data will still be accessible.

Isn't it a cool thing to know?

Keep in mind that a user’s browser on the device they used to access a website is the only place where data stored in LocalStorage is kept. As a result, if visitors return to the same website later using a different browser or on a different device, they will not be able to access the stored data.

How To Access LocalStorage?

It merely requires a few basic steps to access LocalStorage:

  1. By pressing the F12 key on the keyboard, navigate to any website or web application and launch the browser console.
  2. Next, select the Application tab. LocalStorage will be shown under the Storage tab in the menu on the left, as seen below.

3. Click on the LocalStorage dropdown and further click on a/the dropdown item.

As can be seen, there are two columns, keyand value. This is where LocalStorage stores every data you save to it.

How To Use LocalStorage?

setItem()

To store data in LocalStorage, use this technique. A key and a value can both be accepted as their first and second arguments, respectively.

Let’s use the next to add data to LocalStorage.

The JSON.stringify() method must be used to convert objects and arrays to strings since, as was already established, LocalStorage only stores data as strings.

getItem()

Data from LocalStorage can be retrieved using the getItem() method. The key used to save data is the only parameter this procedure accepts.

For instance, you would use the following sentence to access information like the user object mentioned above:

removeItem()

In order to delete data from LocalStorage, use the removeItem() method. A key is an acceptable parameter for this technique.

To remove the user object from LocalStorage, for instance, use the following statement.

key()

To retrieve data from LocalStorage, use the key(index) method, where the index denotes the nth data you wish to retrieve.

clear()

To erase all of the data from LocalStorage at a certain instance, use the clear() function.

Conclusion

I advise you to use LocalStorage in your applications to learn and experiment with it. Become accustomed to storing, locating, and deleting data from it.

Gratitude for reading!

I hope you found this to be interesting to read. Please comment and clap on this article and follow me to get information on new posts.

Cheers!

--

--