Getters, Setters & Property Descriptors
Getters and setters are special functions that run when you read or assign a property, letting you compute values and validate input behind plain property syntax — while property descriptors control whether each property is writable, enumerable, and configurable.
Learn Getters, Setters & Property Descriptors in our free JavaScript course — an interactive lesson with runnable examples, a practice exercise and a quick…
Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
They are how you build computed fields, enforce rules on assignment, and lock specific properties without changing how callers use the object.
📚 Prerequisites: You should know objects, classes, and the basics of how properties work . Getters and setters extend ordinary property access.
⚙️ Real-World Analogy: A getter/setter is like a thermostat dial :
Prefix a function with get or set inside an object literal. A getter runs when you read the property (no parentheses) and is perfect for values derived from other fields. A setter runs when you assign and receives the new value as its single argument — ideal for validation. To callers it all looks like a normal property.
No parentheses: you write person.fullName , not person.fullName() . That's what makes a getter feel like data instead of a method.
Classes use the same get / set keywords. A common pattern stores the real value in a "private" field (a #name or a leading-underscore convention) and exposes a getter/setter pair that guards it. The setter can throw on bad input, so invalid state never gets stored.
Derived getters stay in sync automatically: fahrenheit recomputes from celsius every time it's read, so there's no stale duplicate value to maintain.
Every property has a hidden descriptor. Object.defineProperty lets you create or tune a property with explicit flags: writable (can the value change?), enumerable (does it show in loops and Object.keys ?), and configurable (can it be deleted or redefined?). Properties defined this way default all three to false . Object.getOwnPropertyDescriptor reveals the current settings.
Normal assignment is generous: a property created with obj.x = 1 is writable, enumerable, and configurable by default. defineProperty flips that to locked-down.
Add a getter area that returns width * height .
Predict the output before revealing the answer.
5 — reading o.x runs the getter; note there are no parentheses.
[] — defineProperty defaults enumerable to false .
number — the getter returns the stored number, not a function.
Build an account whose balance setter rejects negative values.
Up next: Proxy & Reflect — intercept every operation on an object. 🛰️
Practice quiz
How do you read a getter property named fullName?
- obj.fullName()
- obj.get(fullName)
- obj.fullName
- obj->fullName
Answer: obj.fullName. A getter is read like a property with no parentheses: obj.fullName.
When does a setter function run?
- When the property is assigned a value
- When the property is read
- When the object is created
- Only inside a loop
Answer: When the property is assigned a value. A setter runs on assignment and receives the new value as its single argument.
For class Temperature with celsius set to 25, what does the fahrenheit getter (#celsius * 9 / 5 + 32) return?
- 25
- 45
- 98.6
- 77
Answer: 77. 25 * 9 / 5 + 32 = 45 + 32 = 77.
Object.defineProperty defaults writable, enumerable, and configurable to what?
- true
- false
- undefined
- null
Answer: false. Properties created with defineProperty default all three flags to false.
After Object.defineProperty(obj, "id", { value: 7 }), what does Object.keys(obj) return?
enumerable defaults to false, so the property does not appear in Object.keys.
Which property descriptor flag controls whether a value can be reassigned?
- enumerable
- configurable
- writable
- readable
Answer: writable. writable controls whether the property's value can be changed.
What does typeof o.n return for const o = { _n: 1, get n() { return this._n; } }?
- "function"
- "number"
- "object"
- "undefined"
Answer: "number". The getter returns the stored number, so typeof is "number".
Why might get name() { return this.name; } cause an infinite loop?
- name is a reserved word
- this is undefined
- name must be a number
- The getter calls itself by reading the same property
Answer: The getter calls itself by reading the same property. Reading this.name re-invokes the getter forever; back it with a different field like this._name.
Which function reveals a property's writable/enumerable/configurable flags?
- Object.keys
- Object.getOwnPropertyDescriptor
- Object.freeze
- Object.assign
Answer: Object.getOwnPropertyDescriptor. Object.getOwnPropertyDescriptor(obj, key) returns the property's descriptor.
If an object has a getter for x but no setter, what happens when you do obj.x = 5?
- It throws immediately in all modes
- It creates a new property
- The assignment is ignored (no setter)
- It runs the getter
Answer: The assignment is ignored (no setter). Without a matching setter, the assignment is ignored.