So far, we have discussed data living in the RAM. But RAM is volatile; when we close the browser, the Heap is erased. To save data or send it to a server, we face a physical problem.
We cannot send a Heap Object over a network cable.
Serialization is the process of converting a complex Tree/Graph Structure (the Object) into a Linear String that can be stored or transmitted.
Serialization : Object to String.Deserialization : String to Object.JSON is the universal standard for serialization in the web. It is language-independent (Python, Java, and C++ all read JSON).
JSON looks like a JS Object, but it has strict architectural constraints to ensure it is parsable by any language.
"key".undefined is not valid JSON.JSON.stringify(data)
Functionor undefined, the engine ignores the entire key-value pair,it is removed from the string.Date objects into ISO Strings (e.g., "2025-12-19T...")..getFullYear() or .setHours().JSON.parse(string)
The browser provides a simple database called localStorage to save data across page reloads.
LocalStorage is effectively a giant Map that lives on the User's Hard Disk (not RAM).
localStorage.setItem('user', obj), the engine implicitly calls .toString() on our object, saving the useless string "[object Object]".To store Objects, we must manually Serialize and Deserialize.
const user = { id: 1, name: "Singh" };
// 1. Write (Serialize)
// RAM Object -> JSON String -> Disk
localStorage.setItem("user", JSON.stringify(user));
// 2. Read (Deserialize)
// Disk -> JSON String -> RAM Object
const storedData = JSON.parse(localStorage.getItem("user"));LocalStorage is Synchronous.
| Feature | JS Object | JSON String |
|---|---|---|
| Location | Heap Memory (RAM) | Variable / Network / Disk |
| Type | Reference Type | Primitive (String) |
| Keys | Identifier / Symbol | Double Quoted String |
| Values | Any (Functions, undefined) | Data Only (No Functions) |
| Circular Refs | Allowed | Throws Error |
| API Method | Direction | Data Loss Risk |
|---|---|---|
JSON.stringify | Object to String | Yes (Funcs/undefined removed) |
JSON.parse | String to Object | No (Strict parsing) |