Testing Objects for Properties (Confused about accessing object values)

Testing Objects for Properties (Confused about accessing object values)
0.0 0


In the following code why doesnt return myObj.checkProp; work?

      var myObj = {
      gift: "pony",
      pet: "kitten",
      bed: "sleigh"
     function checkObj(checkProp) {
      // Your Code Here
        return myObj[checkProp];
      return "Not Found";

    // Test your code by modifying these values

Also i would like the reader to kindly explain in which cases do the `dot notation` not work? Same for  the `bracket notation`.


Dot notation doesn’t work with variables or names with spaces in them. In that case you have to use bracket notation. As far as I know bracket notation can always be used.


Ahha! In the above example, checkProp which’s equal to “gift” has no space in it. Why doesnt it work?


Dot notaion doesnt work with container variables either i guess?


Because it is a variable. So you can use myObj.gift but not myObj.checkProp, because javascript “thinks” it is just text. By using the bracket notation you can use both text: myObj["gift"] and variables: myObj[checkProp].


Hey, he uses bracket notation, but wants to know why dot notation doesn’t work.


@P1xt @BenGitter Thanks guys!


OK, I’m trying to learn when and why you use certain Javascript “formulas”. I was working on this assignment and didn’t have a clue how to do it. I came to the forum and saw the answer and now understand more about dots and brackets, but I don’t understand when to use which “formula.” I got the if else statement part, knew I needed to use that, but the info in the if statement I never would have gotten on my own.

The myObj.hasOwnProperty makes sense, but the (checkProp) does not, to me. In the return, how does the myObj property get associated with the checkProp and then give you the properties under myObj?

I’m really struggling with Javascript and want to start understanding how to figure out which “formula” to use when. I looked at this assignment all weekend and just couldn’t even figure out where to start, except I knew I was going to use and if else statement.


Take for example an object named myObj = {"1" : "one", "2" : "two", "3" : "three"}, anything you see on the left of : is a property and on the right is known as the value. if you need to check whether there exists a certain property In the expression, you use hasOwnProperty() method which takes the intended property as an argument. myObj.hasOwnProperty("5"), will return false since since there is no property names 5 in object. In your question, myObject is not a property but an object. myObj[checkProp] is like doing myObj[“3”] will output “three”. Here object[property] outputs the value.

I hope this clears your confusion.


Okay so from my understanding checkProp is a placeholder that’s why the dot notation didn’t work. I didn’t know that until I came here and saw the explanation. Anyway when checkObj(“gift”) is called; the “gift” part is passed onto checkProp that’s how they’re associated.

For example if you were to put in checkObj(“test”) you would get “Not Found” as a response from checkProp in the function since the properties are being referenced from myObj and “test” doesn’t exist unless added. I hope that clears some things up.

Sorry I know the response is kinda late, but I just started FCC :slight_smile:


It seems unless you “know” dot notation is appropriate, you should just go with bracket notation?

Thanks for the explanations!

Here is my answer:

[spoiler]// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”

function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
} else {
return “Not Found”;
// Test your code by modifying these values


I’d say that’s probably the safest way to go about it.