100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

Getters and Setters in JavaScript

How the `get` and `set` keywords define accessor properties that run code on read/write while still looking like plain property access to callers.

OOP in JavaScriptIntermediate8 min readJul 8, 2026
Analogies

1. Introduction

Getters and setters (accessor properties) let you define object or class properties that run custom logic when read or assigned, while still being used with plain property syntax (obj.prop, not obj.prop()). They are commonly used to validate input on write, compute a derived value on read (such as converting Celsius to Fahrenheit), or add side effects like logging, all without changing how the property appears to the caller.

🏏

Cricket analogy: Think of a scoreboard displaying a batter's run rate — the board doesn't store it directly, it recomputes it from runs and overs each time you glance at it, the way Virat Kohli's strike rate recalculates automatically after each ball without anyone manually updating a stored number.

2. Syntax

javascript
class ClassName {
  #value;

  get propertyName() {
    return this.#value;         // runs on `obj.propertyName`
  }

  set propertyName(newValue) {
    // validate / transform newValue
    this.#value = newValue;     // runs on `obj.propertyName = x`
  }
}

3. Explanation

A get accessor is invoked automatically whenever the property is read (obj.prop), and a set accessor is invoked automatically whenever it is assigned (obj.prop = value) — no parentheses are used at the call site, even though a function actually runs. This makes getters and setters transparent to consumers: code can be refactored from a plain field to a computed accessor without breaking any calling code.

🏏

Cricket analogy: A commentator says 'Rohit Sharma's average' without ever calling a formula out loud — the broadcast system silently runs the calculation behind the scenes whenever the stat is mentioned, just as obj.prop triggers a getter without the caller writing obj.prop().

Getters and setters are commonly paired with a private backing field (#value) so the public accessor name can differ from — or fully control access to — the internal storage. A getter defined without a matching setter makes a property effectively read-only from the outside; assigning to it in non-strict mode silently fails, and in strict mode (including inside ES modules and classes) throws a TypeError.

🏏

Cricket analogy: A player's official contract value (#salary) stays locked in a private file, while the public net-worth figure fans see is a controlled getter — without a setter, journalists can view it but can't edit it, and trying to would just be rejected outright, like strict mode's TypeError.

Getters and setters LOOK like ordinary properties from the caller's side (t.fahrenheit, not t.fahrenheit()), which can be surprising — accessing what appears to be a simple field may actually execute arbitrary logic, throw an exception, or have side effects. Always check for get/set declarations before assuming a property access is 'free' of computation.

4. Example

javascript
class Temperature {
  #celsius;

  constructor(celsius) {
    this.#celsius = celsius;
  }

  get fahrenheit() {
    return this.#celsius * 9 / 5 + 32;
  }

  set fahrenheit(value) {
    this.#celsius = (value - 32) * 5 / 9;
  }

  get celsius() {
    return this.#celsius;
  }

  set celsius(value) {
    if (typeof value !== "number") throw new TypeError("Must be a number");
    this.#celsius = value;
  }
}

const t = new Temperature(25);
console.log(t.fahrenheit);
t.fahrenheit = 98.6;
console.log(t.celsius);
console.log(typeof Object.getOwnPropertyDescriptor(Temperature.prototype, "celsius").get);

try {
  t.celsius = "hot";
} catch (e) {
  console.log(e instanceof TypeError);
}

console.log(t.celsius);

5. Output

text
77
37
function
true
37

6. Key Takeaways

  • get/set define accessor properties that run code but are accessed like plain fields, without parentheses.
  • A getter without a matching setter makes the property effectively read-only from outside the class.
  • Getters and setters are commonly backed by a private #field to fully control internal state.
  • Setters are the natural place to add validation logic, throwing errors for invalid assignments.
  • Accessors are visible on the class prototype and can be inspected with Object.getOwnPropertyDescriptor().

Practice what you learned

Was this page helpful?

Topics covered

#JavaScript#JavaScriptProgrammingStudyNotes#Programming#GettersAndSettersInJavaScript#Getters#Setters#Syntax#Explanation#StudyNotes#SkillVeris