The only data persisted across sessions is the player’s best score, stored inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ac-unefm/snake-game/llms.txt
Use this file to discover all available pages before exploring further.
localStorage under the key snakeBest. Both the read and write operations are wrapped in try/catch blocks so that storage errors — including QuotaExceededError, security restrictions in private-browsing mode, or any other browser-level failure — never crash the game or interrupt play.
readBestScore()
Called once at startup to initialise the best variable and the #bestEl HUD element:
localStorage.getItem('snakeBest')returnsnullwhen the key has never been set. The|| '0'short-circuit converts thatnullto the string'0'before parsing.parseInt(..., 10)ensures the result is always a safe integer, even if the stored value was somehow corrupted.- On any error, the function returns
0and logs to the console. The game starts with a best score of 0 and continues normally.
saveBestScore(score)
Called inside update() each time the player sets a new best score:
- If storage is unavailable (e.g., quota exceeded, private browsing restrictions), the error is caught and logged. The in-memory
bestvariable has already been updated before this call, so the current session’s HUD continues to display the correct value — only persistence fails, silently and without affecting gameplay.
Storage key
The only Reloading the page after running this command will show
localStorage key the game uses is snakeBest. Clearing it resets the best score display to 0 on the next page load. You can reset it manually in the browser console:BEST: 0 in the HUD.When best score is updated
Insideupdate(), immediately after incrementing score when food is eaten:
- The in-memory
bestvariable is overwritten with the new value. - The
#bestElDOM element is updated so the HUD reflects the new best immediately. saveBestScore(best)writes the value tolocalStorage.
saveBestScore throws, both the in-memory state and the visible HUD are already correct for the remainder of the session.