Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Created July 5, 2019 01:12
Show Gist options
  • Save luisenriquecorona/0e94285312b77c875a33196cfe9bca47 to your computer and use it in GitHub Desktop.
Save luisenriquecorona/0e94285312b77c875a33196cfe9bca47 to your computer and use it in GitHub Desktop.
Storage lifetime and scope are explained in more detail below. First, however, let’s look at some examples. The following code uses localStorage, but it would also work with sessionStorage:
var name = localStorage.username; // Query a stored value.
name = localStorage["username"]; // Array notation equivalent
if (!name) {
name = prompt("What is your name?"); // Ask the user a question.
localStorage.username = name; // Store the user's response.
}
// Iterate through all stored name/value pairs
for(var name in localStorage) { // Iterate all stored names
var value = localStorage[name]; // Look up the value of each one
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment