Workshop › Pac-Man-style Maze Chase › Step 41 of 42
Keep the best score even after the page is closed: localStorage is a small store in the browser that survives
reloads. Read it when the game loads, save it when a game ends with a new record, and show it next to the score.
```js
let best = Number(localStorage.getItem('maze-best')) || 0
state = 'over'
if (score > best) {
best = score
localStorage.setItem('maze-best', best)
}
ctx.fillText('Score: ' + score + ' Best: ' + best, 10, 27)
```localStorage.getItem('maze-best') returns the saved text, or null the first time.Number(...) turns it into a number; || 0 uses 0 when there is nothing usable (Number(null) is 0, a broken value
is NaN, and both count as false).setItem saves under the same name.let chain write let best.caught, under state = 'over', write the record block.draw, extend the score text.Skills: Game states
← Harder levels Build it yourself: the slow tunnel →