# Testing Objects for Properties (Confused about accessing object values)

**URL:** <https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028>\
**Category:** General\
**Created:** [June 30, 2016, 8:52pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028 "2016-06-30T20:52:17Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickey005](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mickey005/32/165006_2.png) [@mickey005](https://forum.freecodecamp.org/u/mickey005)\
**Post date:** [June 30, 2016, 8:52pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/1 "2016-06-30T20:52:17Z")

</div>

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

````auto
      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
    checkObj("gift");
```

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

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [June 30, 2016, 8:53pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/2 "2016-06-30T20:53:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mickey005](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mickey005/32/165006_2.png) [@mickey005](https://forum.freecodecamp.org/u/mickey005)\
**Post date:** [June 30, 2016, 8:55pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/3 "2016-06-30T20:55:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mickey005](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mickey005/32/165006_2.png) [@mickey005](https://forum.freecodecamp.org/u/mickey005)\
**Post date:** [June 30, 2016, 8:57pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/4 "2016-06-30T20:57:07Z")

</div>

Dot notaion doesnt work with container variables either i guess?

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [June 30, 2016, 8:58pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/6 "2016-06-30T20:58:54Z")

</div>

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]`.

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [June 30, 2016, 9:00pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/7 "2016-06-30T21:00:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mickey005](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mickey005/32/165006_2.png) [@mickey005](https://forum.freecodecamp.org/u/mickey005)\
**Post date:** [June 30, 2016, 9:15pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/9 "2016-06-30T21:15:41Z")

</div>

@P1xt @BenGitter Thanks guys!

---

<div class="post-metadata">

**Author:** ![Sadaajit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sadaajit/32/8458_2.png) [@Sadaajit](https://forum.freecodecamp.org/u/Sadaajit)\
**Post date:** [July 11, 2016, 3:59pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/10 "2016-07-11T15:59:40Z")

</div>

> [@mickey005](#):
>
> if(myObj.hasOwnProperty(checkProp))  
> return myObj[checkProp];

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.

---

<div class="post-metadata">

**Author:** ![mickey005](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mickey005/32/165006_2.png) [@mickey005](https://forum.freecodecamp.org/u/mickey005)\
**Post date:** [July 22, 2016, 4:46pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/11 "2016-07-22T16:46:13Z")

</div>

Hello,  
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.

---

<div class="post-metadata">

**Author:** ![kuec](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kuec/32/55162_2.png) [@kuec](https://forum.freecodecamp.org/u/kuec)\
**Post date:** [June 20, 2017, 3:35pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/12 "2017-06-20T15:35:39Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![sandsbe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sandsbe/32/59145_2.png) [@sandsbe](https://forum.freecodecamp.org/u/sandsbe)\
**Post date:** [June 23, 2017, 3:13am UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/13 "2017-06-23T03:13:47Z")

</div>

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  
checkObj(“gift”);[/spoiler]

---

<div class="post-metadata">

**Author:** ![kuec](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kuec/32/55162_2.png) [@kuec](https://forum.freecodecamp.org/u/kuec)\
**Post date:** [June 24, 2017, 3:25am UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/14 "2017-06-24T03:25:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 1:09pm UTC](https://forum.freecodecamp.org/t/testing-objects-for-properties-confused-about-accessing-object-values/16028/15 "2021-01-16T13:09:54Z")

</div>

Closed on January 16, 2021, 1:09pm UTC


