Getters and setters
1. Example of a Simple Getter and Setter
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 propertyu.name;set name(value)is called when assigningu.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:
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:
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:
class Temperature {
set celsius(value) {
this.fahrenheit = value * 1.8 + 32;
}
}
const t = new Temperature();
t.celsius = 25;
console.log(t.fahrenheit); // 77This is convenient when you need to "update" internal data on write.
5. Can Be Used With Private Fields
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); // 100Here the private property #balance is protected,
and it's managed through the getter/setter.
6. You Can Compute Values on the Fly
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):
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.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.