Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

When using a backing property in setter method in javascript, why it is necessary to use getter

When using a backing property in setter method in javascript, why it is necessary to use getter method that returns the same backing property. Without getter method why the result is undefinned

1 Answer

Steven Parker
Steven Parker
229,644 Points

A setter method typically does not return anything; so if the return value is tested (as is automatically done by the console), it is normal for it to show "undefined".

A corresponding getter method isn't technically necessary for the setter to work, but is usually necessary for the program that called the setter to retrieve the established value at a later time.