This One JavaScript Feature Can Hide Data (And Most Devs Ignore It)


Can you add a property to an object… and hide it from
for...in
?
Just one line of JavaScript does it.
👀 The Problem
In JavaScript, we often add data to objects like this:
const user = {
name: "dhruv",
password: "12345"
};
But that’s completely exposed. Anyone can user.password
their way in.
Sometimes we want to attach internal data to an object — without exposing it to casual inspection.
Symbol
🔐 What I Discovered
I stumbled upon this while debugging an object in the console and noticing something weird wasn’t showing up:
const pass = Symbol("password");
const user = {
name: "dhruv",
[pass]: "12345"
};
console.log(user.pass); // undefined
console.log(user[pass]); // "12345"
Even though the property is on the object, it's not accessible by dot notation or in normal loops.
Then I tried to loop through the object:
for (let key in user) {
console.log(key); // only logs "name"
console.log(user[key]); // only logs "dhruv"
}
Even though the property is on the object, it's invisible in loops like for...in
and even Object.keys()
.
You just hid a value inside the object and it’s invisible unless you know the exact Symbol
.
🔑 So… What Is a Symbol?
When I saw my property disappear from the loop, I had to dig deeper.
Turns out, Symbol
is a special primitive type in JavaScript — introduced in ES6. It lets you create a unique and hidden key for an object property.
Even if two Symbols look the same, they’re not:
Symbol("secret") !== Symbol("secret");
That uniqueness is the secret sauce. It means you can safely use Symbols to store hidden or internal data, without worrying about accidental overwrites or leaks in loops.
🧠 Why Symbols Matter
Symbols are:
✅ Unique
✅ Non-enumerable by default
✅ Not shown in
for...in
,Object.keys()
, etc.
Even if you use the same description:
Symbol("foo") !== Symbol("foo");
Symbols are great for:
🔒 Hiding internal state
🧩 Customizing object behavior (
Symbol.iterator
, etc.)🚫 Avoiding property name collisions in libraries
🧪 Why this matters in real-world dev:
💡 Ideal for adding debug metadata, internal flags, or hidden settings in dev tools
🧱 They're actually used in Redux, React internals, and even browser APIs
🛡️ Way safer than adding
_secret
keys — those still show up inObject.keys()
,for...in
, etc.
🤯 The Gotcha
Even though Symbols hide data from normal inspection, they’re not truly private:
console.log(Object.keys(user)); // ["name"]
console.log(Object.getOwnPropertyNames(user)); // ["name"]
console.log(Object.getOwnPropertySymbols(user)); // [Symbol(password)]
You literally have to go out of your way to find them.
That’s both powerful… and risky if misused.
✅ When to Use It
Use Symbol()
when you want to:
🔁 Build reusable libraries without naming conflicts
🏷️ Add metadata or internal info to objects (without exposing it)
🧱 Avoid property name clashes in large or shared codebases
🐞 Hide debug-only values that shouldn't show in production
Caution: This is not real security — just a form of obscurity.
🧩 Try It Yourself
Here’s an object with a Symbol
key:
const hidden = Symbol("hidden");
const user = {
name: "Alice",
[hidden]: "🔐 secret"
};
🔍 Your Task:
Try iterating over this object in the following 3 ways:
for...in
loopObject.keys()
Object.getOwnPropertySymbols()
// 1. Classic loop
for (let key in user) {
console.log(key); // What do you see?
}
// 2. Using Object.keys()
console.log(Object.keys(user)); // What’s printed?
// 3. Sneaky peek with Object.getOwnPropertySymbols()
console.log(Object.getOwnPropertySymbols(user));
🧠 Reflect:
Which methods reveal the
Symbol
?Why might that be useful (or dangerous)?
How would you use this to hide internal logic?
This is how JavaScript lets you keep secrets in plain sight.
🧠 TL;DR
Symbol()
creates unique, hidden keysGreat for private-ish object data
Not shown in
Object.keys()
orfor...in
Still accessible if someone digs deep
Like giving your object a secret pocket
If you found this helpful, follow me for more daily JS learnings and dev-friendly explanations! 🧑💻✨
💬 Ever used it in a real project — or thinking about trying it now?
Subscribe to my newsletter
Read articles from Dhruv Burada directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
