FAQ: Advanced Objects - Getters

This community-built FAQ covers the “Getters” exercise from the lesson “Advanced Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Getters

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Please could someone explain why the end of this lesson the answer is:


and not


and when to know when to put the brackets in please?

Hi, I am just learning about getters and setters myself and without seeing your code I can’t be sure,
but I believe that when calling getter and setter methods, the function is called without ().


This makes sense actually I think they did mention that somewhere and I forgot! Thank you for your help

get attribute () {
    return this._attribute;

We write a get call the way we would access an object property, normally.


We know there is no literal attribute by that name, but a backing variable with an underscore, which value is returned by the getter. We never invoke the getter method as we would a typical method, since it is self-invoking and accesses the actual attribute and returns it.

Likewise, we never invoke a setter, but call it like we would be making an assignment.

object.attribute = newValue;

The mechanics behind the scene are such that we cannot access (get) a variable while it is being set. It’s a bit complicated to fully understand, let alone explain, but warrants further study as you get more immersed in the finer details. Add this to your reading list.

I’m currently at this lesson, and I can’t really understand why my solution is not accepted. Could someone please explain?

const robot = {
_model: ‘1E78V2’,
_energyLevel: 100,
get energyLevel() {
if (typeof this._energyLevel === ‘number’) {
return My current energy level is ${this._energyLevel}.;

I checked the solution, and while I understand it, it’s also strange they are using a different way of calling in the properties than in the example just above the exercise.


I actually figured it out after a few revisits, my solution was not accepted because ‘My current energy level is ${this._energyLevel}.’ had a period at the end. This is what you get when don’t follow the instructions to the … period :wink:


I don’t understand why using getters is necessary. I tested the code, and it works exactly the same if you declare energyLevel() as a regular function, and then when you log it, you just have to call it with parenthesis, like a standard function.

Codecademy provided a few reasons for using getters:

  • Getters can perform an action on the data when getting a property.
  • Getters can return different values using conditionals.
  • In a getter, we can access the properties of the calling object using this .
  • The functionality of our code is easier for other developers to understand.

The only one that seems legitimate to me is the last reason, simply that using the get keyword can help make your code aka your intention, more readable to others. But can’t you achieve the first three things on that list with a standard function?


Given that without a getter, all we can do is poll the attribute for its value,


anything else would require a special method which we would have to invoke on the value. Not insurmountable, but will take some thought.

Using a getter, we can perform an action (some process) and/or dress up the return by containing not just the polled value, but other data associated with the attribute or value.

Again, without a getter, we would need a method to evaluate the conditional(s) and return it.

In both the above cases one can envision a couple of static class methods filling in for those roles. We need to dig for some examples, which I am hard pressed to drum up off the top of my head.


I see. I guess I was confused because the example provided in the exercise didn’t seem to require a getter for the desired result.


OMG thats what it was for me too, dang period:tongue:

Glad I could be of help :wink:

■■■■■■■ that period! I spent about 20 minutes trying to figure out what I’d done wrong!

1 Like

@mtf and @yekusiel or any one pls, I don’t see the difference btw a getter and a method. Can anyone help me understand this two thing.

1 Like

All getters are methods, but not all methods are getters. Only those specially declared with get are getters, and they always return the value of the variable to which they are associated. Their purpose is pretty well defined.


You can add values to a object i.e

const myObj = {
name: ‘Magnus’

myObj.age = 29;

Then age will be added to the object right.

But is it possible to add a getter and a setter function from outside the object ?


Right. Any property that does not exist will be created and assigned the value. If the property does exist, it will take on the new value.

Not that I am aware of. Outside of the object we can add functions, but not methods.

myObj.func = x => return x ** 2;

or, the more conventional approach would be to use function so we have a scoped this object.

myObj.getName = function() {
    return `My object name is ${this.name}.`;
1 Like

Why does typeof this._energyLevel === ‘number’ evaluate to true when the data type of _energyLevel is 100 not a string ‘number’

1 Like

The comparison is not this._energyLevel === ‘number’, but, typeof this._energyLevel === ‘number’. typeof is evaluated before the comparison.

typeof 100 => 'number'
1 Like

typeof this._energyLevel === ‘number’
In this statement as @mtf stated, typeof is evaluated first, and returns the data type as a string. In this case typeof returns ‘number’ and since ‘number’ === ‘number’ the statement is true.
Check this out:

1 Like