JSON & localStorage — saving data
2 min read
What is JSON, really?
JSON (JavaScript Object Notation) is just a way to write data as text, so it can travel over the internet or be saved. It looks like JS objects, but everything is text.
Two tools convert between them:
let user = { name: "Riya", age: 25 };
let text = JSON.stringify(user); // object → text
console.log(text); // '{"name":"Riya","age":25}'
let back = JSON.parse(text); // text → object
console.log(back.name); // Riya
JSON.stringify→ object to text (for saving or sending).JSON.parse→ text back to object (for using).
Saving data with localStorage
localStorage lets your site remember data even after the page is closed — like a tiny storage box in the browser.
localStorage.setItem("username", "Riya"); // save
let name = localStorage.getItem("username"); // read
localStorage.removeItem("username"); // remove
localStorage only stores text, so to save an object, stringify it first:
localStorage.setItem("user", JSON.stringify(user));
let saved = JSON.parse(localStorage.getItem("user"));
Why this matters
This is how a site remembers your theme choice, your cart, your login — without a server. Combine fetch, JSON and localStorage, and you can build real, data-driven apps.
JSON = data as text. stringify to save, parse to use. localStorage remembers it across visits.