Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active May 1, 2020 13:49
Show Gist options
  • Save ross-u/c7ae162b3c0b876ef89d56b131055789 to your computer and use it in GitHub Desktop.
Save ross-u/c7ae162b3c0b876ef89d56b131055789 to your computer and use it in GitHub Desktop.
localStorage - example 01/20
function updateScore(name, score) {
// GET PREVIOUS SCORE
let previousScore = JSON.parse(localStorage.getItem("score"));
if (!previousScore) {
previousScore = [];
}
// UPDATE THE SCORE
const newScore = { name, score };
previousScore.push(newScore);
// SET THE SCORE BACK TO LOCAL STORAGE
const updatedScoreStr = JSON.stringify(previousScore);
localStorage.setItem("score", updatedScoreStr);
}
updateScore("bob", 123);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment