0Pricing
JavaScript Academy · Lesson

Storing Objects with JSON

Serialize objects to store and retrieve them.

Storage Holds Only Strings

Web Storage stores strings, so you cannot save an object directly. Storing one yields the useless text "[object Object]". The fix is JSON.

localStorage.setItem("user", { name: "Ada" });
console.log(localStorage.getItem("user")); // "[object Object]"

Serialize With JSON.stringify

Convert an object to a JSON string with JSON.stringify before storing it.

const user = { name: "Ada", age: 36 };
localStorage.setItem("user", JSON.stringify(user));

All lessons in this course

  1. Storing and Reading Data
  2. localStorage vs sessionStorage
  3. Storing Objects with JSON
  4. Storage Events and Limits
← Back to JavaScript Academy