Public Properties by Default
You'll learn how JavaScript objects expose all properties and methods publicly without built-in access control.
Everything Is Open
Unlike languages like Java or C++ that have private, protected, and public keywords, JavaScript historically made every property and method on an object accessible from outside. When you create an object—whether using an object literal, constructor function, or class—all its properties and methods are public by default.
This means anyone with a reference to your object can read, modify, or delete any of its properties:
const user = {
name: "Alice",
password: "secret123"
};
console.log(user.password); // "secret123" - fully accessible!
user.password = "hacked"; // Can be changed
delete user.password; // Can even be deleted
Why This Matters
This default openness means you can't rely on language-level protection to hide implementation details. If you create an object with sensitive data or internal state, there's nothing stopping other code from reaching in and messing with it.
Think of it like leaving your house unlocked: JavaScript assumes you'll coordinate with your neighbors (other developers) rather than installing locks (access modifiers). This "open by default" philosophy puts responsibility on conventions and patterns rather than enforced restrictions.
Over time, JavaScript has added workarounds—closures for privacy in factories, symbols, and more recently private fields in classes—but these are additions to work around the default public nature, not replacements for it.
Key Takeaway: JavaScript objects expose all properties and methods publicly by default; unlike statically-typed languages, there are no built-in access modifiers to restrict visibility, making encapsulation a matter of pattern choice rather than language enforcement.