Suggest an editImprove this articleRefine the answer for “What does the private access modifier do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`private`** makes a class property or method accessible only inside the class itself - not from outside, and not in derived classes. **Key point:** this is checked only by TypeScript at compile time and disappears in the compiled JS, so real runtime privacy requires `#private`.Shown above the full answer for quick recall.Answer (EN)ImageThe `private` modifier in TypeScript is used to **hide** a class property or method, making it **accessible only inside the class itself**. This is a means of **encapsulation** that helps protect internal data from outside interference. --- ## 1. Syntax ```javascript class User { private password: string; constructor(password: string) { this.password = password; } private encryptPassword(): string { return this.password.split("").reverse().join(""); } public checkPassword(pwd: string): boolean { return this.encryptPassword() === pwd.split("").reverse().join(""); } } const u = new User("secret"); u.checkPassword("secret"); // ok u.password; // Error - private u.encryptPassword(); // Error - private ``` Everything marked `private` is **accessible only inside the class itself** (not from outside, and not in subclasses). --- ## 2. Visibility rules | Where you access it from | Can you access `private`? | |---|---| | Inside the class | Yes | | In a subclass | No | | From outside (via an instance) | No | --- ## 3. An example with inheritance ```javascript class Animal { private type = "animal"; public showType() { console.log(this.type); } } class Dog extends Animal { bark() { // console.log(this.type); Error - private in Animal } } const dog = new Dog(); dog.showType(); // works through the public method ``` > `private` protects data even from **subclasses**. > If you need it visible in derived classes, use `protected`. --- ## 4. Private fields in constructor parameters ```javascript class User { constructor(private name: string, private age: number) {} public getInfo() { return `${this.name}, ${this.age}`; } } const u = new User("Tim", 30); u.getInfo(); // Tim, 30 u.name; // Error: private ``` TypeScript creates and types the private fields itself if you specify `private` directly in the constructor parameter. --- ## 5. The difference between `private` (TS) and `#private` (JS) TypeScript supports **two kinds of privacy**: | Kind | Level of privacy | Checked | Available at runtime | |---|---|---|---| | `private` | Only at the **TypeScript** level | at compile time | No (removed in JS) | | `#private` | A real JavaScript field (ES2022) | by the JS engine | Yes (actually private) | ### Example: ```javascript class Account { private balance = 100; // TS-only privacy #id = 42; // JS-level privacy getInfo() { console.log(this.balance, this.#id); } } const a = new Account(); a.balance; // Type error (but accessible at runtime via a["balance"]) a.#id; // Error at both the type level and the JS level - truly hidden ``` `#private` works even after compilation - it is real protection at runtime, not just in TS. --- ## 6. Combining `private` + `readonly` Sometimes fields are made **private and read-only**: ```javascript class Config { private readonly secret = "key123"; } ``` > The value cannot be changed even inside the class after initialization. --- ## Summary | Characteristic | Description | |---|---| | Accessibility | Only inside the class | | Access from subclasses | No | | Access from outside | No | | Level of protection | Checked by TypeScript at compile time | | Alternative | `#private` - a real private field in JS | | Common use | For hiding logic, passwords, tokens, internal data | --- In short: > `private` protects a class's internal data from outside code > and makes it accessible **only inside the class itself**.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.