Suggest an editImprove this articleRefine the answer for “Getters and setters”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Getters** (`get`) and **setters** (`set`) are special class methods that look like regular properties: `get name()` is called when reading `obj.name`, and `set name(value)` when assigning `obj.name = ...`, and inside them you can run checks or computations. **Key point:** if you define only a getter, the property becomes read-only, and if you define only a setter, it becomes write-only; getters and setters pair well with private fields (`#field`), giving controlled access to protected data.Shown above the full answer for quick recall.Answer (EN)Image## 1. Example of a Simple Getter and Setter ```javascript class User { constructor(name) { this._name = name; // a "private" property (by convention) } get name() { console.log("Getter called"); return this._name; } set name(value) { console.log("Setter called"); if (value.length < 2) throw new Error("Name is too short"); this._name = value; } } const u = new User("Alice"); console.log(u.name); // Getter called → "Alice" u.name = "Alex"; // Setter called console.log(u.name); // "Alex" ``` Here: - `get name()` is called when **accessing the property** `u.name`; - `set name(value)` is called when **assigning** `u.name = "..."`; - inside them you can run checks, computations, and so on. --- ## 2. How This Works Under the hood, `get` and `set` are **special property descriptors**, defined via `Object.defineProperty`. In other words, these methods **don't store a value**, they **manage it**: ```javascript u.name; // → calls get name() u.name = x; // → calls set name(x) ``` --- ## 3. A Getter Without a Setter (Read-Only) If you only define a getter, the property becomes **read-only**: ```javascript class Circle { constructor(radius) { this.radius = radius; } get area() { return Math.PI * this.radius ** 2; } } const c = new Circle(10); console.log(c.area); // 314.159... c.area = 100; // Nothing happens (there's no setter) ``` Getters are often used for computed properties (for example, `area`, `fullName`, `priceWithTax`, and so on). --- ## 4. A Setter Without a Getter (Write-Only) If you only need to react to a **change**, you can create a setter without a getter: ```javascript class Temperature { set celsius(value) { this.fahrenheit = value * 1.8 + 32; } } const t = new Temperature(); t.celsius = 25; console.log(t.fahrenheit); // 77 ``` This is convenient when you need to "update" internal data on write. --- ## 5. Can Be Used With **Private Fields** ```javascript class Account { #balance = 0; get balance() { return this.#balance; } set balance(value) { if (value < 0) throw new Error("Balance cannot be negative"); this.#balance = value; } } const acc = new Account(); acc.balance = 100; console.log(acc.balance); // 100 ``` Here the private property `#balance` is protected, and it's managed through the getter/setter. --- ## 6. You Can Compute Values on the Fly ```javascript class User { constructor(first, last) { this.first = first; this.last = last; } get fullName() { return `${this.first} ${this.last}`; } set fullName(value) { const [first, last] = value.split(" "); this.first = first; this.last = last; } } const u = new User("Alice", "Fisher"); console.log(u.fullName); // "Alice Fisher" u.fullName = "Alex Smith"; console.log(u.first); // "Alex" console.log(u.last); // "Smith" ``` Here, the `fullName` property looks like a regular field, but it actually calls the `get` and `set` methods. --- ## 7. Can Be Used With `Object.defineProperty()` Getters and setters can also be set manually (outside a class): ```javascript const user = { _name: "Alice" }; Object.defineProperty(user, "name", { get() { return this._name.toUpperCase(); }, set(value) { this._name = value.trim(); } }); user.name = " alex "; console.log(user.name); // "ALEX" ``` This is the same mechanism a class uses under the hood. --- ## Comparison With Regular Properties | Characteristic | Regular Property | Getter/Setter | | --- | --- | --- | | Stores a value | Yes | No | | Called like a function | No | Automatically | | Can add logic | No | Yes | | Supports private fields | Yes | Yes | | Looks like a regular property | Yes | Yes | --- ## In Short > Getters (`get`) and setters (`set`) are a class's "smart properties", > that let you **control the reading and writing of data**. > > They: > > - look like regular fields; > - allow validation, computation, and logic; > - also work with private properties; > - don't require extra parentheses when called.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.