Hi there, I'm Marcos!

💽 Storing data in the browser with localStorage

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

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

// Remove item from localStorage
window.localStorage.removeItem('key');

// Remove all items from localStorage
window.localStorage.clear();

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.