Skip to main content

Getters and setters

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

CharacteristicRegular PropertyGetter/Setter
Stores a valueYesNo
Called like a functionNoAutomatically
Can add logicNoYes
Supports private fieldsYesYes
Looks like a regular propertyYesYes

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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.