Mid From PDF JavaScript JavaScript

Can you use getters and setters in classes?

Yes, getters and setters control access to object properties.

Example:

class Person {
constructor(name) { this._name = name; }

get name() { return this._name; }

set name(value) { this._name = value; }
}
const p = new Person("Sandeep");

console.log(p.name); // Sandeep

p.name = "Ravi";

console.log(p.name); // Ravi

More from JavaScript Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details