HomeTutorsContact
JavaScript
How to use localStorage in web application using JavaScript?
Gulshan Saini
July 07, 2020
1 min

localStorage allows web applications to store and access data on the client-side right in the browser using JavaScript. Data stored in the local storage by the web application can be accessed by current origin only.

Window.localStorage or simply localStorage stores data forever until it is cleared by the application. This is different than sessionStorage where the storage is cleared when browser is closed. Also, the data remain in sync if the user has multiple tabs open of the same application.

Let’s explore various methods that are available under localStorage API.

Storing data in localStorage

localStorage.setItem() method allows storing data in key & value pairs where both key and value should be in String format. To store JSON object in local storage first we need to convert that in a string using JSON.stringify() method.

Example

Accessing localStorage items

To access data from localStorage we can use method localStorage.getItem(). The getItem() method accepts key in string format as argument. If the key is not found null is returned

Example

Removing localStorage item

To remove localStorage item we can use method localStorage.removeItem(). The removeItem() method accepts key in string format as an argument.

Example

Clearing local storage

To clear local storage we can use clear() method. This will clear all the local storage items.

Example


Tags

#html
Previous Article
How to launch the Firefox browser using puppeteer?

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Related Posts

What Is HTML 5 Doctype?
July 29, 2020
1 min
© 2020, All Rights Reserved.

Quick Links

Advertise with usContact Us

Social Media