---
title: "What are the set and get property in class of JS?"  
description: "What are the set and get property in class of JS?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93754/what-are-the-set-and-get-property-in-class-of-js  
category: "web application"  
tags: ["javascript", "web application development", "web development"]  
reading_time: 2 minutes  

---

# What are the set and get property in class of JS?

What are the set and get [property](https://www.mindstick.com/articles/44059/couple-of-supportive-tips-when-taking-a-gander-at-property-for-sale) in [class](https://yourviews.mindstick.com/view/81038/business-class-of-israel-raged-with-coronavirus-pandemic) of JS?

## Answers

### Answer by Ethan Karla

Getters and setters allow you to define Object Accessors (Computed Properties). Accessor Property. In JavaScript, accessor properties are methods that get or set the value of an object. **For that, we use these two keywords:** get - to define a getter method to get the property value set - to define a setter method to set the property value **JavaScript Getter** In JavaScript, getter methods are used to access the properties of an object. For example,

```
const student = {
    firstName: 'Ravi vishwakarma',
    // accessor property(getter)
    get getName() {
        return this.firstName;
    }
};
console.log(student.firstName); // Ravi vishwakarma
console.log(student.getName); // Ravi vishwakarma
console.log(student.getName()); // error
```

In the above program, a getter method getName() is created to access the property of an object. **JavaScript Setter** In JavaScript, setter methods are used to change the values of an object. For example,

```
const student = {
    firstName: 'Ravi vishwakarma',
    //accessor property(setter)
    set changeName(newName) {
        this.firstName = newName;
    }
};
console.log(student.firstName); // Ravi vishwakarma// change(set) object property using a setter
student.changeName = 'Water fall';
console.log(student.firstName); // Water fall
```

**JavaScript Object.defineProperty()** In JavaScript, you can also use Object.defineProperty() method to add getters and setters. For example,

```
const student = {
    firstName: 'Ravi vishwakarma'
}
// getting property
Object.defineProperty(student, 'getName', {
    get : function () {
        return this.firstName;
    }
});
// setting property
Object.defineProperty(student, 'changeName', {
    set : function (value) {
        this.firstName = value;
    }
});
console.log(student.firstName); // Ravi vishwakarma
// changing the property value
student.changeName = 'Water fall';
console.log(student.firstName); // Water fall
```

In the above example, Object.defineProperty() is used to access and change the property of an object. The syntax for using Object.defineProperty() is: Object.defineProperty(obj, prop, descriptor) The Object.defineProperty() method takes three arguments. The first argument is the objectName. The second argument is the name of the property. The third argument is an object that describes the property. \


---

Original Source: https://answers.mindstick.com/qa/93754/what-are-the-set-and-get-property-in-class-of-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
