📚 JavaScript Adventure: const vs Object.freeze() — The Tale of the Unchangeable Toy List
🏰 Welcome to Playville: A Kingdom of Code and Chaos
Once upon a push of a keyboard in the cheerful land of Playville, lived a flock of hardworking messenger birds. These birds had a very serious job — they flew from one village to another, delivering toys to children around the world.
Their delivery system was magical and worked perfectly… until one day, it didn’t. 😱
Someone started changing toy names — calling “Doll” a “Princess” and adding strange new toys like a “Banana Blaster”! The birds were confused. These weren’t the toys they packed! 😖🎁🍌
Children cried. Birds squawked. Toy deliveries were delayed. 😢
So the wise Toy Council met in their code castle and said:
“We must protect the official toy list! It must never change. Not by accident. Not even by magic.”
And that, young coder, is how we stumbled into one of the most important lessons in JavaScript:
The difference between const
and Object.freeze()
🪄 The First Spell: const
— The Name Protector
The royal programmer stepped forward and wrote:
const ToyBox = {
Car: 1,
Doll: 2,
Robot: 3
};
This was a great start. const
meant that no one could come and say:
ToyBox = { Banana: 4 }; // ❌ ERROR! Can’t replace the box.
The name ToyBox
was protected — you couldn’t assign a new value to it anymore.
But here’s where things got tricky…
Even though the name ToyBox
was safe, the inside of the box could still be changed. Anyone could open the lid and sneak in new stuff!
ToyBox.Doll = 99; // ✅ The Doll just changed!
ToyBox.Banana = 4; // ✅ A banana snuck in!
delete ToyBox.Car; // ✅ The Car was stolen!
So, while const
locked the box name, it didn’t protect what was inside the box.
The birds were still confused. The chaos continued. And that’s when the second, more powerful spell was cast…
❄️ The Second Spell: Object.freeze()
— The Inside Protector
The royal coder took the list of toys and whispered a stronger incantation:
const ToyBox = Object.freeze({
Car: 1,
Doll: 2,
Robot: 3
});
BOOM! 💥
A glass box formed around the toy list — clear enough to see through, strong enough that no one could mess with it.
Now:
- ❌ You couldn’t change a toy’s name or number
- ❌ You couldn’t add a new toy
- ❌ You couldn’t even delete a toy!
Try it:
ToyBox.Banana = 4; // ❌ Ignored
ToyBox.Doll = 99; // ❌ Ignored
delete ToyBox.Car; // ❌ Still there!
Together, const
and Object.freeze()
made sure:
- ✅ You can’t replace the box (
const
) - ✅ You can’t tamper with the box’s content (
freeze
)
The birds were thrilled. The kids got their proper toys. Playville rejoiced.
💡 So, What’s An Enum?
In programming, an Enum (short for enumeration) is a fixed list of values that your code uses over and over.
In Playville terms, it’s a list like this:
const ToyTypes = Object.freeze({
Car: 1,
Doll: 2,
Robot: 3
});
This ensures that:
- When you say
ToyTypes.Doll
, it always means2
- No one can accidentally change it to
ToyTypes.Doll = "Princess"
Enums give us:
- ✅ Consistency
- ✅ Safety
- ✅ Peace of mind
Especially when used with both const
and Object.freeze()
.
🔍 Quick Comparison: const
vs Object.freeze()
Feature | const Only |
const + Object.freeze() |
---|---|---|
Can reassign value? | ❌ No | ❌ No |
Can change contents? | ✅ Yes | ❌ No |
Can add/delete keys? | ✅ Yes | ❌ No |
Protects the object? | ❌ Not fully | ✅ Fully |
Best for | Locking variable name | Creating Enums & safe configurations |
🧠 Review Quiz — Test Your Magic!
-
What does
const
protect in JavaScript? A. The values inside an object B. The name of the variable C. The color of your code -
What does
Object.freeze()
do? A. Makes an object look cool B. Stops your computer from heating up C. Locks all properties inside an object so they can’t change -
What happens if you try to add a new property to a frozen object? A. It adds it anyway B. It ignores you C. Your code sings a song 🎵
-
Which setup gives you full protection for an Enum-like object? A.
let Toys = {}
B.const Toys = {}
C.const Toys = Object.freeze({})
-
Which of the following is a correct example of a frozen Enum? A.
let list = ["Car", "Doll", "Robot"];
B.Object.freeze({ Car: 1, Doll: 2, Robot: 3 })
C.const toys = new Array();