Reading list Switch to dark mode

    Getter Setter in Javascript

    Updated 30 December 2019

    Getter and Setter are introduced in the ECMAScript 5. In JavaScript, Getter and Setter are the most powerful property to set the values of the object and you want to get the value which depends on the other values in the object. We need to use get and set keyword at the start of the method when we are declaring the getter and setter method. Getter and setter is the accessor property rather than the method. For example, you have a customer object with the first name and last name in the object and you want to get the value of the customer, then get the method will provide you the complete name of the customer.

    P10


    In this we did not need to call the getter method as a function, We just need to use it as a variable of the object only.

    In this, If you have created a getter method only then we can get the value of the object only, We can not set the values by declaring the setter method in the object.

    P11

    To make it work, We need to define the setter method as well.

    P12

    As we can check that fullname is virtual property of the object, With the help of the getter and setter, it is readable and writeable now.

    Start your headless eCommerce
    now.
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home