Using Local and Session Storage

Save data

  • Using setItem() save data to local and session storage Retrieve data
  • Using getItem() retrieve data from local and session storage Grab and display all data
  • Using a for loop to iterate over each key/value pair in local and session storage Delete data from storage
  • Using removeItem() delete a key/value pair from local and session storage
  • Using clear() delete all data from local and session storage JSON data and local and session storage
  • Using JSON.stringify() to save JSON objects to web storage
  • Using JSON.parse() to retrieve JSON objects from web storage

Square bracket syntax .property syntax


localStorage[“myLocalData”] = “Sample Data Here” let data = localStorage[“myLocalData”];

Outdated Web Storage Use This is the square bracket syntax, which is deprecated and shouldn’t be used.


localStorage.myLocalData = “Sample Data Here” let data = localStorage.myLocalData;

Outdated Web Storage Use This is the .property syntax, which is deprecated and shouldn’t be used.


localStorage.setItem(‘key’, ‘value’) sessionStorage.setItem(‘key’, ‘value’) localStorage.getItem(‘key’) sessionStorage.getItem(‘key’)


Summary

Save/retrieve data

  • setItem()
  • getItem() Using .key() loop through all data Remove data
  • removeItem()
  • clear() Storing JSON
  • stringify()
  • parse() Deprecated usage
Previous