# Getters and Setters

**URL:** <https://forum.kirupa.com/t/getters-and-setters/640148>\
**Category:** programming\
**Created:** [August 7, 2019, 7:41pm UTC](https://forum.kirupa.com/t/getters-and-setters/640148 "2019-08-07T19:41:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 7, 2019, 7:41pm UTC](https://forum.kirupa.com/t/getters-and-setters/640148/1 "2019-08-07T19:41:16Z")

</div>

The properties we have been working with so far are known as _ **data properties** _. These are the properties where we give them a name and assign a value to them:

* * *
This is a companion discussion topic for the original entry at [https://www.kirupa.com/javascript/getters\_setters.htm](https://www.kirupa.com/javascript/getters_setters.htm)

---

<div class="post-metadata">

**Author:** ![REB\_412](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/reb_412/32/9233_2.png) [@REB\_412](https://forum.kirupa.com/u/REB_412)\
**Post date:** [August 7, 2019, 11:18pm UTC](https://forum.kirupa.com/t/getters-and-setters/640148/2 "2019-08-07T23:18:59Z")

</div>

Nice summary article. Your readers should find i it very useful.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 8, 2019, 3:34am UTC](https://forum.kirupa.com/t/getters-and-setters/640148/3 "2019-08-08T03:34:43Z")

</div>

Thanks! You can credit @senocular for making me write it as part of addressing feedback on the upcoming edition of the _ **JavaScript: A Beginner’s Guide** _ 🏝

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [April 8, 2020, 7:02am UTC](https://forum.kirupa.com/t/getters-and-setters/640148/5 "2020-04-08T07:02:23Z")

</div>

And…there is a video I recorded to go along with this tutorial:

[![](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/3/f/3fcb29a6073b137bf2ffb7b003e338f567045e57.jpeg "Getters and Setters in JavaScript") ](https://www.youtube.com/watch?v=d9Uqz2BHHk8)

📹

---

<div class="post-metadata">

**Author:** ![burnella](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/burnella/32/12340_2.png) [@burnella](https://forum.kirupa.com/u/burnella)\
**Post date:** [October 30, 2020, 10:43pm UTC](https://forum.kirupa.com/t/getters-and-setters/640148/6 "2020-10-30T22:43:36Z")

</div>

Hi There. I have some questions about the use of underlines in this Getters and Setters tutorial. I’ve Googled around to understand why one would use a \_ at the beginning of a variable in JS and I find chatter about the underline being an indicator of a private variable. I think in this instance you are using it for a different reason maybe? Using the Shout Generator section as a basis for these questions, I see you create a \_message property and create a setter named ‘set message’. If I throw out putting a \_ in the message property, then the this.message = value.toUpperCase(); creates strange behavior (like recursive). If I leave it in, I see later when I issue shout.message = “This is sparta!”; that the setter is called. So, we are somewhat hiding the implementation of the setter, right? We are not directly setting \_message, but secretly calling the setter? Also, the setter doesn’t necessarily have to have the same name as the property it sets as in the zorb example, right? I’m trying to understand the sudden appearance of the underline before the property name in this section.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [October 31, 2020, 12:17am UTC](https://forum.kirupa.com/t/getters-and-setters/640148/7 "2020-10-31T00:17:33Z")

</div>

> I’ve Googled around to understand why one would use a \_ at the beginning of a variable in JS and I find chatter about the underline being an indicator of a private variable. I think in this instance you are using it for a different reason maybe?

It’s related to the explanations you’ve found. Kirupa’s creating some backing storage for the getters and setters, but it’s an implementation detail, so it doesn’t need to be public.

> [@burnella](#):
>
> I see you create a \_message property and create a setter named ‘set message’. If I throw out putting a \_ in the message property, then the this.message = value.toUpperCase(); creates strange behavior (like recursive).

Something subtle between the `zorb` example and the `shout` example is that in the `zorb` example, the backing property is named `message`, but the getters and setters are called `greeting`. By contrast, in the `shout` example, the backing property is `_message` and the getters and setters are `message`. When you take out the leading `_`, you’re creating a naming conflict so that the `set message` setter calls itself in its own definition, leading to the recursive behavior you described.

> [@burnella](#):
>
> If I leave it in, I see later when I issue shout.message = “This is sparta!”; that the setter is called. So, we are somewhat hiding the implementation of the setter, right? We are not directly setting \_message, but secretly calling the setter?

The part that’s hidden is the name (and storage) of the `_message` property. The public interface is the accessors (getters and setters) that don’t start with `_`. So you’re not hiding the fact that you’re calling the setter, you’re hiding what the setter is doing in its implementation.

> [@burnella](#):
>
> Also, the setter doesn’t necessarily have to have the same name as the property it sets as in the zorb example, right?

Right; it’s almost always going to be a bug if it does.

> [@burnella](#):
>
> I’m trying to understand the sudden appearance of the underline before the property name in this section.

I think the main problem is that Kirupa swapped from `message`+`greeting` to `_message`+`message` without explaining the rationale.

---

<div class="post-metadata">

**Author:** ![Marco\_L](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/marco_l/32/22593_2.png) [@Marco\_L](https://forum.kirupa.com/u/Marco_L)\
**Post date:** [July 5, 2023, 1:00am UTC](https://forum.kirupa.com/t/getters-and-setters/640148/8 "2023-07-05T01:00:15Z")

</div>

Hi Kirupa,

thank you so much for all your effort and help with the Tutorials.  
I just want to say, that your YouTube video is not the correct on the JavaScript 101, getter and setter [Getters and Setters](https://www.kirupa.com/javascript/getters_setters.htm)!  
It should be the [Getters and Setters in JavaScript - YouTube](https://youtu.be/d9Uqz2BHHk8)  
Best regards,  
Marco

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 5, 2023, 1:40pm UTC](https://forum.kirupa.com/t/getters-and-setters/640148/9 "2023-07-05T13:40:49Z")

</div>

Hi @Marco_L - glad you liked the content! The links you shared seem to be the same. One is the video itself and the other is the article with the video embedded. Am I missing an important detail? 😀

Thanks,  
Kirupa

---

<div class="post-metadata">

**Author:** ![Marco\_L](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/marco_l/32/22593_2.png) [@Marco\_L](https://forum.kirupa.com/u/Marco_L)\
**Post date:** [July 5, 2023, 2:00pm UTC](https://forum.kirupa.com/t/getters-and-setters/640148/10 "2023-07-05T14:00:08Z")

</div>

Hi Kirupa, maybe it was due to a cookie or something similar, but just to be completely sure, I tried using another browser, and the Youtube video in the Getter and Setter article did not display the correct video. It’s likely a bug on my end!

By the way, the visuals and explanation are really great! 👍
