Unit 7 · Data that lasts
Why your variables reset when you refresh
A variable lives in the memory of one open page. A refresh starts that page from scratch, so every variable starts over. To keep data, save it in the browser’s storage or, if others need it, a database.
The mix-up
What people often think
“A variable keeps its value until I change it.”
What the tutor says
A variable lives in the page's memory. A refresh starts the page from scratch, so every variable starts over too.
How it works
This is not a bug. Memory is fast and temporary by design; storage is slower and lasting. Decide for each piece of data how long it needs to live and who needs to see it.
Close cousins of this mix-up
The tutor watches for these in the same lesson.
“Data in a JavaScript variable stays there.”
A variable lives only while that tab is open. Reload, and it is gone.
Practise it with a tutor watching
Why a database
Say where a piece of data should live: page memory, the browser, or a database on a server.
Free with an account. The first two units need no account.