Site icon

Storing and Clearing Objects in localStorage javascript

Storing and Clearing Objects in localStorage javascript

Hello Everyone Today in this blog we are going to know about the Storing and Clearing Objects in localStorage javascript. So, first of all, Web storage objects localStorage allow saving key/value pairs in the browser.

The main features of localStorage are:

You May Also Like:

Storage objects provide the same methods and properties:

localStorage Example

var testObject = { 'one': 1, 'two': 2, 'three': 3 };

// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));

Looping Over Keys

As we’ve seen, the methods provide “get/set/remove by key” functionality. But how to get all saved values or keys? Unfortunately, storage objects are not iterable. One way is to loop over them as over an array:

for(let i=0; i<localStorage.length; i++) {
  let key = localStorage.key(i);
  alert(`${key}: ${localStorage.getItem(key)}`);
}

Summary

Storing and Clearing Objects in localStorage in javascript is very easy to use.

Objects in localStorage allow storing key/value in the browser.

Storage event:

Thank you for reading this blog, share this blog as much as possible and also show it to your friends. If you have any questions, feel free to comment.

Exit mobile version