DEV Community

loading...
Cover image for Javascript Local Storage

Javascript Local Storage

rrtutors profile image rrtutors Originally published at rrtutors.com ・1 min read

Javascript Local storage example we will cover how to store and retrieve data from local storage. What is local storage? Local storage properties allows user to store data in key value pairs in the web browser. This local storage object stores the data without expiration date. This local storage data will not be deleted when we close the window. This local storage will be a read only property

How to store data in Local Storage?

To store data on Local Storage browser we need to use localStorage object. To store the each data item we need to pass key and value to the setItem() method.

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

Alt Text

How to fetch data from Local Storage?

To fetch the data from the local storage first we need to fetch all keys first then fetch the values by passing the key to localStorage.getItem(key) method.

for(let k=0;k {
const key=localStorage.key(k);
const value=localStorage.getItem(key);
outputinp.innerHTML +=key+":"+value+"
";

}
Enter fullscreen mode Exit fullscreen mode

How to delete data from Local Storage?

To delete the specific key item data from the local storage we will use localStorage.removeItem(key) method by passing the key name.

localStorage.removeItem("key Name")
Enter fullscreen mode Exit fullscreen mode

Download Javascript Local storage source code

Discussion (0)

Forem Open with the Forem app