Teach Your App to Remember!
What if your game could remember your high score, even after you close it? Today, we're going to build a magic memory box for our apps so they never forget!
๐ง What is Browser Memory?
Imagine your web browser has a secret backpack. It's called localStorage! You can put little notes inside it, like 'Highest Score: 50'. The browser will keep that note safe in its backpack, even if you turn off the computer. When you come back, the note is still there!
Try the Magic Memory Box! ๐ฆ
Type your name below and click the 'Remember Me!' button. Then, try refreshing the page. See? It remembers you!
Build a High-Score Clicker Game! ๐ช
You've seen how it works, now let's build something fun! We're going to create a simple "cookie clicker" style game that remembers your score every time you come back.
Step 1: The HTML Foundation ๐๏ธ
First, we need a button to click and a place to show our score.
<button id="clicker-button">Click me!</button>
<h2>Score: <span id="score-display">0</span></h2>
Step 2: Make it Clickable with JavaScript! โจ
Now, let's add some JavaScript to make the score go up with each click.
let currentScore = 0;
const clickerButton = document.getElementById('clicker-button');
const scoreDisplay = document.getElementById('score-display');
clickerButton.addEventListener('click', () => {
currentScore++;
scoreDisplay.innerText = currentScore;
});
Step 3: The Secret Sauce - localStorage! ๐พ
This is where the magic happens! Every time the score changes, we'll save it to our browser's secret backpack (localStorage). Think of it like a school locker: setItem puts something *in* the locker with a label, and getItem uses that label to get it back *out*.
clickerButton.addEventListener('click', () => {
currentScore++;
scoreDisplay.innerText = currentScore;
// Save the new score to localStorage!
localStorage.setItem('myGameScore', currentScore);
});
Step 4: Load Your Saved Score ๐
Finally, when the page first loads, we need to check the backpack to see if there's already a score saved.
// When the page loads...
window.addEventListener('load', () => {
const savedScore = localStorage.getItem('myGameScore');
if (savedScore) {
currentScore = parseInt(savedScore); // Convert string back to a number
scoreDisplay.innerText = currentScore;
}
});
Deep Dive: Saving More Than Just Text ๐ง
LocalStorage is great, but it has a secret: it only likes to store strings! What if you want to save a whole player profile with a name, a score, and a list of items? That's where JSON (JavaScript Object Notation) comes in. It's a way to turn your data objects into a string to save them, and turn them back into an object when you load them.
// An object with lots of data
const playerProfile = {
name: "Pan",
level: 5,
inventory: ["sword", "shield"]
};
// Turn the object into a string to save it
localStorage.setItem('playerData', JSON.stringify(playerProfile));
// Get the string back and turn it into an object again!
const loadedDataString = localStorage.getItem('playerData');
const loadedProfile = JSON.parse(loadedDataString);
console.log(loadedProfile.name); // Prints "Pan"
Remember the Privacy Shield! Even when using JSON, never save real passwords, addresses, or other sensitive personal information in localStorage.
Add In-Game Upgrades!
Modify the clicker game to not only save the score, but also an 'upgrades' object. Add a button that lets you 'buy' an upgrade (like 'Auto-Clicker') which gets saved to localStorage using JSON. When the page loads, check if the user owns the upgrade and change the game accordingly!
๐จโ๐ฉโ๐ง Parent Corner
The "localStorage" feature your child is using is specific to their browser on this one device. It's a great, safe way to learn about data persistence! Conversation Starter: Ask your child to explain how the game remembers their score. Then, talk about the difference between data saved on your computer (like this) versus data saved online (like in a multiplayer game account).
๐ง Concept Checkpoint
Which line of code correctly SAVES the player's score of 100 into the browser's memory?
localStorage.getItem('score', 100);localStorage.setItem('score', 100);browser.remember('score', 100);๐ Learn More
- MDN Docs: The Official `localStorage` Guide - The professional's handbook.
- CodePen: `localStorage` Projects - See how others use it to build cool stuff.
- Next Level: Cloud Storage with Firebase - When you need to save data online so you can access it from any device. (Note: Requires a Google account and parent supervision!)