Web Storage

  1. Overview
    1. Web Storage API allow JavaScript programs to securely store key/value pairs in the web browser
    2. sessionStorage object stores key/value pairs that are only available for the duration of the session (browser closing ends session)
    3. localStorage object stores key/value pairs indefinitely
    4. Values are not accessible outside of an origin: scheme, hostname, port number
  2. Accessing data
    1. setItem(key, value) stores the key string and associated value string in storage
      localStorage.setItem("name", "Bob");
      
    2. getItem(key) the key's value or null if the key does not exist
      // Returns "Bob"
      let name = localStorage.getItem("name");
      
    3. removeItem(key) removes the key and associated value from storage
      // Remove name and value 
      localStorage.removeItem("name");
      
    4. clear() removes all keys and associated values from storage
      // Remove all
      localStorage.clear();
      
  3. View data with Chrome DevTools
    1. Open DevTools and select Application tab, then look for Storage in bottom-left