0

I just started learning javascript using codecademy and I'm doing a quiz on object properties.

One of the questions is as follows:

"Which of the following lines of code would print the value saved to the _num key in the tempObj object?"

The code is:

let tempObj = {
    _num: 22,
    get num() {
       return this._num;
  }
};

One of the correct answers was console.log(tempObj['num']);

However, I'm confused by this because wouldn't adding the quotation marks around 'num' make the computer evaluate it as a string rather than as a getter? If not, why not?

  • 1
    *"wouldn't adding the quotation marks around 'num' make the computer evaluate it as a string"* - Indeed it does. And it uses that string to access a property on the object using "bracket notation". So `tempObj.num` is essentially the same as `tempObj['num']`. – David Sep 16 '22 at 18:54
  • To follow-up on what David said and complete the answer, the getter is used either way, whether `tempObj.num` or `tempObj['num']`. `tempObj[num]` would not be valid in this context. – Codebling Sep 16 '22 at 18:57

0 Answers0