How Can be add and remove data from local storage:-

Data is added to local storage using “key” and “value”. Below sample code shows country data “India” added with key value “Key001”.

localStorage.setItem("Key001","India");

To retrieve data from local storage we need to use “getItem” providing the key name.

var country = localStorage.getItem("Key001");

You can also store JavaScript object’s in the local storage using the below code.

var country = {};
country.name = "India";
country.code = "I001";
localStorage.setItem("I001", country);
var country1 = localStorage.getItem("I001");

If you want to store in JSON format you can use “JSON.stringify” function as shown in the below code.

localStorage.setItem("I001",JSON.stringify(country));

results matching ""

    No results matching ""