Problem access to an object with the dot operator

Hi,
Hi, I would like to know why do I get undefined when I use the dot to access to “myObj”; why do I need to use the square brackets instead of the dot. Thanks for the answer in advance :smiley:

My Code:

// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};

function checkObj(checkProp) {
// Only change code below this line
if (myObj.hasOwnProperty(checkProp)) {
  return myObj.checkProp; //HERE IS THE QUESTION
} else {
return "Not Found";
}
// Only change code above this line
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums