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 01
Storage chapter. An app is more useful when it remembers data. But browser memory (and files, and the network) holds only TEXT — so first you must tell JSON from a JS object.
Theory. A JS object is a live value in memory: it has properties and you can change it. JSON is text in a strict format: keys in double quotes, no functions, no undefined, no comments. JSON.stringify(object) turns an object into such text; JSON.parse(text) turns it back.
Small examples:
const obj = { name: "Rent", amount: 500 }; const text = JSON.stringify(obj); typeof obj; // "object" typeof text; // "string" text.length; // length of the text, not a property count
JSON text is a plain string — you can save it, but text.amount is undefined.
Common mistake: thinking JSON is an object; reading a property from a JSON string; expecting functions and Date to survive.
<p id="raw"></p><p id="kinds"></p><p id="size"></p>JSON is a string (it has length); an object is a value with properties — different types.
(empty)Press Run — HTML loads in a sandbox iframe; Stop removes the iframe (also kills infinite loops).
›