đź’˝ Storing data in the browser with localStorage

Wed Jun 16 2021

// Add item to localStorage
window.localStorage.setItem('key', 'value');

// Get item from localStorage
const value = window.localStorage.getItem('key');

// Remove item from localStorage

// Remove all items from localStorage

You can store data in the browser to be retrieved at a later time. You'll need a key to reference the stored value that we'll be used on read time. Here are the different methods available:

  • Add item: localStorage.setItem(key, value)
  • Get item: localStorage.getItem(key)
  • Remove item: localStorage.removeItem(key)
  • Remove all items: localStorage.clear()

Only string values can be stored but you can convert arrays or objects into JSON to bypass this limitation.