Getter Methods

Introduction

Getter methods are special methods in JavaScript that allow you to read or retrieve the value of a property in a controlled way. They are an important part of encapsulation because they provide access to private or internal data without exposing it directly.

Instead of accessing a property directly, a getter method can perform additional tasks such as validation, formatting, calculations, or hiding sensitive information before returning the value.

Getter methods are created using the get keyword.

For automation engineers, getter methods are commonly used to retrieve configuration values, browser information, page titles, API URLs, test data, authentication details, and other internal values while keeping the actual implementation hidden.

In this tutorial, you’ll learn how to create and use getter methods in Node.js.


What are Getter Methods?

A getter method is a special method that returns the value of a property.

It is defined using the get keyword and can be accessed like a normal property without parentheses.


Why Use Getter Methods?

Getter methods help developers:

  • Protect internal data.

  • Support encapsulation.

  • Control how values are accessed.

  • Format data before returning it.

  • Improve code readability.

  • Hide implementation details.

  • Improve maintainability.


Syntax

class Employee {

    constructor(name) {

        this._name = name;

    }

    get name() {

        return this._name;

    }

}

const employee =
    new Employee("John");

console.log(employee.name);

Example 1: Basic Getter Method

class Student {

    constructor(name) {

        this._name = name;

    }

    get name() {

        return this._name;

    }

}

const student =
    new Student("David");

console.log(student.name);

Sample Output

David

Notice that student.name is accessed like a property, not as student.name().


Example 2: Getter with Private Field

class Employee {

    #salary;

    constructor(salary) {

        this.#salary = salary;

    }

    get salary() {

        return this.#salary;

    }

}

const employee =
    new Employee(50000);

console.log(employee.salary);

Sample Output

50000

Example 3: Formatting Data

class User {

    constructor(firstName, lastName) {

        this.firstName = firstName;

        this.lastName = lastName;

    }

    get fullName() {

        return this.firstName +
            " " +
            this.lastName;

    }

}

const user =
    new User(
        "Rahul",
        "Sharma"
    );

console.log(user.fullName);

Sample Output

Rahul Sharma

Example 4: Calculated Value

class Rectangle {

    constructor(length, width) {

        this.length = length;

        this.width = width;

    }

    get area() {

        return this.length *
            this.width;

    }

}

const rectangle =
    new Rectangle(10, 5);

console.log(rectangle.area);

Sample Output

50

Example 5: Real-World Example

Display employee information.

class Employee {

    constructor(id, name) {

        this.id = id;

        this.name = name;

    }

    get employeeInfo() {

        return this.id +
            " - " +
            this.name;

    }

}

const employee =
    new Employee(
        101,
        "Sophia"
    );

console.log(employee.employeeInfo);

Sample Output

101 - Sophia

Automation Testing Example

Getter methods are widely used in automation frameworks.

Playwright Example

Retrieve the application URL.

class BasePage {

    constructor() {

        this._baseUrl =
            "https://example.com";

    }

    get baseUrl() {

        return this._baseUrl;

    }

}

const page =
    new BasePage();

console.log(page.baseUrl);

Selenium Example

Retrieve the browser name.

class BrowserConfig {

    constructor() {

        this._browser =
            "Chrome";

    }

    get browser() {

        return this._browser;

    }

}

const config =
    new BrowserConfig();

console.log(config.browser);

Cypress Example

Retrieve the environment name.

class TestConfig {

    constructor() {

        this._environment =
            "QA";

    }

    get environment() {

        return this._environment;

    }

}

const config =
    new TestConfig();

console.log(config.environment);

API Testing Example

Retrieve the API endpoint.

class ApiClient {

    #baseUrl =
        "https://api.example.com";

    get baseUrl() {

        return this.#baseUrl;

    }

}

const api =
    new ApiClient();

console.log(api.baseUrl);

Data-Driven Testing Example

Retrieve test user information.

class TestUser {

    constructor(username) {

        this._username =
            username;

    }

    get username() {

        return this._username;

    }

}

const user =
    new TestUser("admin");

console.log(user.username);

Common Mistakes

Calling a Getter Like a Method

Incorrect:

console.log(student.name());

Correct:

console.log(student.name);

Getter methods are accessed like properties.


Returning Nothing

Incorrect:

get name() {

}

A getter should return a value.


Modifying Data Inside a Getter

A getter should retrieve data, not modify it. Use a setter method if you need to update a property.


Best Practices

  • Use getters to provide controlled access to properties.

  • Keep getter methods simple.

  • Return meaningful values.

  • Use getters with private fields when appropriate.

  • Avoid performing expensive operations inside getters.

  • Use descriptive getter names.

  • Combine getters with setters when controlled read and write access is required.


Conclusion

Getter methods provide a clean and controlled way to access object data while supporting encapsulation. They allow developers to expose information without giving direct access to internal properties.

For automation engineers, getters are commonly used to retrieve browser configurations, page information, API endpoints, test data, and application settings while keeping implementation details hidden.

Understanding getter methods prepares you for using setter methods to safely update object data.


Frequently Asked Questions (FAQs)

What is a getter method?

A getter method is a special method used to retrieve the value of a property.


How is a getter declared?

A getter is declared using the get keyword.


How do you access a getter?

A getter is accessed like a normal property without parentheses.


Can getters work with private fields?

Yes. Getters are commonly used to return the values of private fields.


Why are getter methods useful in automation testing?

They provide controlled access to configuration values, API endpoints, browser information, page data, and other reusable values while hiding internal implementation details.


Key Takeaways

  • Getter methods retrieve property values.

  • Getters are declared using the get keyword.

  • Access getters like properties, not methods.

  • Getters improve encapsulation.

  • They provide controlled access to internal data.

  • Getters can return formatted or calculated values.

  • They work well with private fields.

  • Getter methods are widely used in Playwright, Selenium, Cypress, and API testing frameworks.

  • Keep getters simple and focused on returning data.

  • Getter methods are commonly paired with setter methods for controlled data access.