Getters, Setters & Static Methods0%

Getters, Setters & Static Methods

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Getters, Setters & Static Methods in Modern JavaScript

Modern JavaScript classes provide accessor properties (get and set) for computed values and data validation, alongside static methods and properties that belong to the class constructor itself rather than to individual instances.


1. Accessor Properties: Getters and Setters

Getters and setters bind an object property to a function that is invoked whenever that property is read or assigned. They resemble standard properties syntactically while providing encapsulation:

JavaScript
class Temperature {
constructor(celsius) {
this._celsius = celsius; // Internal convention
}
 
// Getter: Invoked when reading temp.celsius
get celsius() {
return this._celsius;
}
 
// Setter: Invoked when assigning temp.celsius = value
set celsius(value) {
if (typeof value !== 'number') {
throw new TypeError('Temperature must be a numerical value.');
}
if (value < -273.15) {
throw new RangeError('Temperature cannot be below Absolute Zero (-273.15°C).');
}
this._celsius = value;
}
 
// Computed Getter: Fahrenheit
get fahrenheit() {
return (this._celsius * 9) / 5 + 32;
}
 
// Setter for Fahrenheit updates internal Celsius!
set fahrenheit(val) {
this.celsius = ((val - 32) * 5) / 9;
}
}
 
const weather = new Temperature(25);
console.log(weather.celsius); // 25
console.log(weather.fahrenheit); // 77
 
weather.fahrenheit = 212; // Updates via setter
console.log(weather.celsius); // 100

2. Static Methods and Properties

The static keyword defines methods and properties that exist on the class constructor itself, not on instance objects. They are ideal for utility functions, factory methods, or shared constants:

JavaScript
class User {
// Static Property (ES2022)
static MIN_PASSWORD_LENGTH = 8;
 
constructor(username, email) {
this.username = username;
this.email = email;
}
 
// Static Factory Method: Constructs User from JSON API response
static fromJSON(jsonString) {
const data = JSON.parse(jsonString);
return new User(data.name, data.email);
}
 
// Static Utility: Compares two users
static areEqual(userA, userB) {
return userA.email.toLowerCase() === userB.email.toLowerCase();
}
}
 
// Access static members via the Class name:
console.log(User.MIN_PASSWORD_LENGTH); // 8
 
const user1 = User.fromJSON('{"name": "Elena", "email": "elena@example.com"}');
console.log(user1.username); // "Elena"
 
// Instances CANNOT access static methods:
// user1.fromJSON(...); // TypeError: user1.fromJSON is not a function
Output
Class Architecture:
User Constructor ──► [ static fromJSON(), static areEqual() ]
│
new User()
│
▼
user1 Instance ──► [ username, email ] ──► User.prototype [ instance methods ]

3. Static Inheritance

In JavaScript, static members are inherited through the prototype chain of constructor functions:

JavaScript
class BaseService {
static getServiceName() {
return 'Base Core Service';
}
}
 
class AuthService extends BaseService {}
 
console.log(AuthService.getServiceName()); // 'Base Core Service' (Inherited statically!)

Interactive Quiz

Practice Quiz

Question 1
How do you access a getter property defined as get fullName() on an instance user?
Question 2
What is the primary purpose of a setter method?
Question 3
Can an instance of a class directly call a static method (e.g. instance.myStaticMethod())?
Question 4
What common design pattern is frequently implemented using static class methods?
Question 5
What error occurs if a setter attempts to assign to the exact same property name without a backing field (e.g. set score(val) { this.score = val; })?
Next Lesson

Private Fields & Methods (#field)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext