DEV Community

professional writer
professional writer

Posted on

JavaScript Getter and Setter

A getter method is a special type of method that is used to retrieve the value of a property from an object. This is different from the regular methods, which take a parameter and return a value , also Getter and setter in Node js.

The getter method is called when you want to get the value of the property. The method doesn't do anything with the value that it gets , and React js Getter and Setter.

The getter method is always anonymous. That is to say, you don't need to provide a name for the getter method. You can also use an empty parentheses when you call the getter method. This syntax is called Explicit Call Syntax. You can also use the same syntax when you call the setter method. The setter method is called when you want to set the value of the property. The setter method doesn't do anything with the value that it sets.

Data Property

const student = {

    // data property
    firstName: 'Monica';
};
Enter fullscreen mode Exit fullscreen mode

JavaScript Getter

const student = {

    // data property
    firstName: 'Monica',

    // accessor property(getter)
    get getName() {
        return this.firstName;
    }
};

// accessing data property
console.log(student.firstName); // Monica

// accessing getter methods
console.log(student.getName); // Monica

// trying to access as a method
console.log(student.getName()); // error
Enter fullscreen mode Exit fullscreen mode

There are two types of getter methods. The first type of getter method is called getter and the second type of getter method is called setter.

You can also use the getter and setter methods together. That is to say, you can have both a getter and a setter method in the same object.

There are also two types of setter methods. The first type of setter method is called setter and the second type of setter method is called mutator.

The getter and setter methods are always assigned to the property. That is to say, the getter and setter methods are defined before the property is declared.

The getter and setter methods are read-only. That is to say, you can't change the value of the property using the getter and setter methods.

The getter and setter methods are static. That is to say, they are always available no matter where you are in the code.

  1. There are two types of getter and setter methods. The first type of getter and setter methods is called getter and setter methods. The second type of getter and setter methods is called getter and setter methods with a block.
  2. The getter and setter methods with a block are called getter and setter methods with an explicit block. This is the same as the getter and setter methods that have empty parentheses.
  3. The getter and setter methods with an explicit block are called getter and setter methods with an explicit block. This is the same as the getter and setter methods that have a name.
  4. The getter and setter methods with a name are called getter and setter methods with a named block.
  5. The getter and setter methods with a named block are called getter and setter methods with a named block.
  6. The getter and setter methods with a named block are called getter and setter methods with a named block.
  7. The getter and setter methods with a named block are called getter and setter methods with a named block.
  8. The getter and setter methods with a name are called getter and setter methods with a named block.
  9. There are two types of getter and setter methods. The first type of getter and setter methods is called getter and setter methods with a block. The second type of getter and setter methods is called getter and setter methods with an explicit block.
  10. The getter and setter methods with an Explicit block are called getter and setter methods with an Explicit block. This is the same as the getter and setter methods that have empty parentheses.
  11. You can also use the getter and setter methods together. That is to say, you can have both a getter and a setter method in the same object.
  12. There are also two types of setter methods. The first type of setter method is called setter and the second type of setter method is called mutator.
  13. The getter and setter methods are always assigned to the property. That is to say, the getter and set

JavaScript has a getter and setter method which is used to get or set the value of a property in an object. The getter method is called when you want to get the value of the property, and the setter method is called when you want to set the value of the property.

Top comments (0)