DEV Community

Tanishk Sharma
Tanishk Sharma

Posted on

What is localStorage?

localStorage is a way to store data on the client-side, in the browser. This data is stored in key/value pairs and is available to JavaScript code running on that page.

localStorage is persistent, meaning that the data will remain even if the browser is closed and reopened. This makes it a good choice for storing data that needs to be accessed later, such as user preferences or settings.

It's important to know that the data stored in localStorage is specific to the domain, so data stored on one site will not be available to another site.

localStorage is used to store data in key/value pairs. The setItem() method is used to store data:

localStorage.setItem('key', 'value');
Enter fullscreen mode Exit fullscreen mode

The getItem() method is used to retrieve data:

localStorage.getItem('key');
Enter fullscreen mode Exit fullscreen mode

The removeItem() method is used to remove data:

localStorage.removeItem('key');
Enter fullscreen mode Exit fullscreen mode

The clear() method is used to clear all data from localStorage:

localStorage.clear();
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
bellatrix profile image
Sakshi

Informative

Collapse
 
andrewbaisden profile image
Andrew Baisden

Nice simple intro thanks for contributing to the community.