JavaScript classes have come a long way. Back in the old days (2015 π), developers used all sorts of workarounds β closures, naming conventions, and even comments β to pretend something was private.
But today?
JavaScript finally gives us real private fields using the # symbol.
Letβs explore this in a fun, human way!
π§© What Are Public & Private Fields?
Think of a class like a house:
- Public fields = your living room
β Anyone can enter and see it. - Private fields = your secret cupboard
β Only the house owner (the class itself) can open it.
JavaScript now lets us create these secret cupboards cleanly.
π Public Fields (Easy & Friendly)
Public fields are visible to anyone using your class.
class User {
name = "Amit"; // public field
greet() {
console.log(`Hello, I am ${this.name}!`);
}
}
const u = new User();
u.greet(); // Hello, I am Amit!
console.log(u.name); // Accessible β
Everything is open and accessible β great for normal data.
π Private Fields Using # (Modern, Clean & Safe)
Private fields are created by adding a # before the field name.
class BankAccount {
#balance = 0; // private field
deposit(amount) {
this.#balance += amount;
}
getBalance() {
return this.#balance;
}
}
const acc = new BankAccount();
acc.deposit(500);
console.log(acc.getBalance()); // 500
console.log(acc.#balance); // β ERROR: Private field
#balance can only be accessed inside the class β perfect for sensitive data.
β‘ Why Private Fields Are Better (vs old tricks)
Old ways developers tried to make things private:
- Using
_underscore(just a naming convention π) - Using closures (complex)
- Using
Symbol()(hacky) - Using WeakMaps (overkill)
Private fields with #:
β Real privacy
β Faster
β Clean syntax
β Supported in all modern browsers
π― Real-Life Example: Login System
class Login {
#password;
constructor(username, password) {
this.username = username;
this.#password = password;
}
authenticate(inputPassword) {
return this.#password === inputPassword;
}
}
const user = new Login("Amit", "secret123");
console.log(user.authenticate("wrong")); // false
console.log(user.authenticate("secret123")); // true
console.log(user.#password); // β Nope
This keeps your password hidden β like it should be!
π οΈ Example: Rate Limiter (Prevent Too Many Requests)
class RateLimiter {
#limit = 5;
#requests = 0;
request() {
if (this.#requests < this.#limit) {
this.#requests++;
return "Request allowed β";
}
return "Too many requests β";
}
}
const api = new RateLimiter();
console.log(api.request());
console.log(api.request());
console.log(api.#limit); // β Private
Perfect for APIs and backend logic.
π§ When Should You Use Private Fields?
Use private fields when you want to protect:
- Passwords
- Tokens
- Internal counters
- Calculations
- Anything users shouldnβt directly modify
Public fields are fine for:
- Display names
- UI-visible data
- Non-sensitive properties
π Final Thoughts
Private fields (#name) are one of JavaScriptβs best modern additions.
They keep your code clean, secure, and future-proof.
Use them when you want strict privacy.
Use public fields when you want flexibility.
By mixing both, you get the perfect class design β strong, readable, and safe.

Leave a Reply