JavaScript Getters and Setters
A getter is a method that runs when a property is read. A setter is a method that runs when a property is written.
A getter works out a value
area is a getter, not a stored field. Reading box.area runs the method and returns 20.
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
get area() {
return this.width * this.height;
}
}
const box = new Rectangle(4, 5);
console.log(box.area);A setter stores the value
The width setter stores each new value in _width. The getter reads _width back. The width becomes 10.
class Rectangle {
constructor(width) {
this.width = width;
}
set width(value) {
if (value <= 0) {
throw new RangeError("Width must be above zero");
}
this._width = value;
}
get width() {
return this._width;
}
}
const box = new Rectangle(4);
box.width = 10;
console.log(box.width);A setter refuses a bad value
The width setter throws a RangeError for any value at or below zero. The bad write is caught, and the width stays 4.
class Rectangle {
constructor(width) {
this.width = width;
}
set width(value) {
if (value <= 0) {
throw new RangeError("Width must be above zero");
}
this._width = value;
}
get width() {
return this._width;
}
}
const box = new Rectangle(4);
try {
box.width = -3;
} catch (err) {
console.log(err.name);
console.log(err.message);
}
console.log(box.width);A getter without a setter is read-only
Circle has an area getter and no setter. Writing to area is ignored, so the area stays 12.56.
class Circle {
constructor(radius) {
this.radius = radius;
}
get area() {
return 3.14 * this.radius * this.radius;
}
}
const circle = new Circle(2);
circle.area = 99;
console.log(circle.area);Example
class Score {
constructor(points) {
this.points = points;
}
set points(value) {
if (value < 0 || value > 100) {
throw new RangeError("Points must be between 0 and 100");
}
this._points = value;
}
get points() {
return this._points;
}
get grade() {
if (this._points >= 90) {
return "A";
}
return "B";
}
}
const score = new Score(95);
console.log(score.points);
console.log(score.grade);
score.points = 80;
console.log(score.grade);Key points
- Getter: a method that runs when a property is read
- Setter: a method that runs when a property is written
- A getter starts with get, and a setter starts with set.
- A getter looks like a plain property to the code that reads it.
- A setter can check a new value before it is stored.
- A setter can refuse a bad value by throwing an error.
- A getter can work out a value from other properties, instead of storing it.
In this example, the Score class guards its points with a setter. The setter refuses any value outside 0 and 100. The grade getter works out a letter from the stored points.
Try it Yourself
Edit the code, then press Run.
Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.
Exercise
What runs when you read a property that has a getter?