In the previous article, (check it out if you have not gone through 👉 Shallow Copy Vs Deep Copy) , we had discussed about a concept at the end.
"There are multiple ways to create a deep copy. But we now know the real problem—objects are passed by reference! 🤯
💡 What if... we could temporarily transform a non-primitive object into a primitive type 🧪, pass it by value ➡️, and then rebuild it back into an object? 🔄
Sounds like a hack, right? The processes involved in this concept is called serialization and deserialization . Let’s take a perfect analogy
You just finished writing your homework assignment đź“ť, and tomorrow is the submission deadline.
But there's a problem!
Your friend, who also needs to submit the same assignment, lives far away and has a fractured right hand 🤕. He can’t write, and there’s no way you can go to his house to give him your notebook.
This is just like how objects in JavaScript can’t be sent directly over the internet.
Step 1: Serialization (Converting to a Transferable Format) 📤
You think of taking photos of each page and sending them one by one. But wait—there are too many pages! 📄📄📄
Taking a picture of each page individually would take too much time.
Instead, you decide to scan all the pages into a single PDF file 📄➡️📂.
Now, instead of sending multiple loose pages, you have one compact file that can be easily transferred.
🔸 How Does This Relate to JavaScript?
In JavaScript, we use Serialization to convert objects into a format that can be transferred.
let a = { subject: "Math", pages: ["page1", "page2", "page3"] };
let b = JSON.stringify(a); // Convert to a transferable format
This process is called Serialization—turning an object into a string so it can be easily transferred.
But why exactly a string? Why not just wrap it inside another object or array?
Reason 1:
We studied in the previous articles that
Objects & Arrays in JavaScript are passed by reference
Strings are passed by value
We don't want to share the same object because that would create a shallow copy, meaning changes to one object would affect the other!
But if we convert it into a string, we break the reference.
let a = { subject: "Math", pages: ["page1", "page2", "page3"] };
let b = a; // ❌ Shallow copy (same reference)
friendassignment.subject="Science";
console.log(friendassignment.subject); // ❌ Science
Reason 2: Strings Are Universal (Can Be Sent Over the Internet)
Think about sending data over a network (Internet, APIs, databases, etc.).
Strings are easy to store and transmit 📡
Every programming language understands strings
Objects and Arrays cannot be directly sent over HTTP requests
🔴 You can’t send this object directly over the internet:
{ name: "Raghav", age: 20 }
âś… But you can send it as a string:
"{\"name\": \"Raghav\", \"age\": 20}"
Now it can be sent, stored, and later converted back into an object.
Reason 3: JSON Format is Lightweight and Standardized
JSON (JavaScript Object Notation) is based on strings
It’s the most common format for sending data between systems
It’s smaller in size compared to raw objects, making it more efficient
JSON looks like a list of key-value pairs, similar to an object in JavaScript:
{ subject: "Math",
pages: ["page1", "page2", "page3"] };
Now that your homework is in PDF format, you send it over WhatsApp to your friend.
Similarly, in JavaScript, we send serialized objects (strings) over the internet, APIs, or storage.
Step 3: Deserialization (Rebuilding the Homework) đź“Ą
Your friend receives the PDF and downloads it đź’ľ.
But he still needs a physical copy to submit! So, he prints the PDF 🖨️ and now has a brand-new assignment that looks exactly like yours.
How Does This Relate to JavaScript?
In JavaScript, we use Deserialization to convert a string back into an object.
let c = JSON.parse(b); // Convert back to an object
Even though the content is the same, your friend's notebook is a new physical copy—not your original notebook!
Take a lot at how memory is managed during serialization and deserialization
let a = { subject: "Math", pages: ["page1", "page2", "page3"] };
let b = JSON.stringify(a);
let c = JSON.parse(b);
console.log(c) //{ subject: 'Math', pages: [ 'page1', 'page2', 'page3' ] }
Thankyou!!