Getter and Setter methods in JS
In JavaScript, getter methods are used to get the value of an object by using get keyword and setter methods are used to set the value of an object by using set keyword.
<script>
class Person {
constructor(name) {
this.name = name;
}
// getter
get personName() {
return this.name;
}
// setter
set personName(newName) {
this.name = newName;
}
}
let person1 = new Person(‘Shairy’);
console.log(person1.name);
// changing the value of name property
person1.personName = ‘Kalra’;
console.log(person1.name);
</script>