Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
JAVASCRIPT STORAGE / CHAPTER 06 / Learn / LESSON 02
Theory. The path "object → text → object" is called a round-trip. After JSON.parse you get a NEW object with the same data — not the same one. That is why copy === original is false even if they look equal.
JSON.parse expects valid JSON. The result holds only what JSON can carry: numbers, strings, booleans, null, arrays, plain objects. A Date becomes a string; undefined and functions disappear.
Small examples:
const text = '{"id":1,"title":"Rent","amount":500}'; const data = JSON.parse(text); data.title; // "Rent" JSON.stringify(data) === text; // true for this compact form
Key order is kept, spaces are not: stringify(data) gives compact text unless you pass an indent.
Common mistake: comparing objects with ===; expecting text with single quotes to parse; parsing an already parsed object.
<pre id="json">{"id":1,"title":"Rent","amount":500,"tags":["home"]}</pre><p id="out"></p><p id="tags"></p><p id="back"></p>parse returns a new object; stringify of it gives the same compact text.
(empty)Press Run — HTML loads in a sandbox iframe; Stop removes the iframe (also kills infinite loops).
›