A getter is a method that gets the value of a specific property. It allows you to access a property using a function, but it appears as if you are accessing a regular property. Getters can be useful for data validation, logging, or computation on-the-fly.

Syntax

let obj = {
  get propertyName() {
    // code to get the property value
  }
};

Example

let person = {
  firstName: 'John',
  lastName: 'Doe',
  get fullName() {
    return `${this.firstName} ${this.lastName}`;
  }
};

console.log(person.fullName); // Output: John Doe

Usage