# I'm Stuck on Testing Objects for Properties

**URL:** <https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [July 12, 2016, 2:28pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710 "2016-07-12T14:28:55Z")\
**Posts on this page:** 15\
**Page:** 1

<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 12, 2016, 2:28pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/1 "2016-07-12T14:28:55Z")

</div>

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 this 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? Could someone give me their logical train of thought that helped them to know what coding to use?

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:** ![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 12, 2016, 3:53pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/3 "2016-07-12T15:53:58Z")

</div>

Thanks. I appreciate you helping me out. It will probably help a lot of us that struggle with Javascript. HTML and CSS were a breeze, but I feel like I’ve entered an alien world with Javascript.

---

<div class="post-metadata">

**Author:** ![sethkoch](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@sethkoch](https://forum.freecodecamp.org/u/sethkoch)\
**Post date:** [July 12, 2016, 4:13pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/4 "2016-07-12T16:13:31Z")

</div>

I’m not sure what a formula is. hasOwnProperty is a method that all objects have. A method is a function that is contained in an object. You can’t see hasOwnProperty inside myObj, but it’s there, it’s contained in its parent object, object.prototype.

checkProp is the parameter set in the function, so when the function is invoked with an argument, that argument is referred to as checkProp in the function. The function then calls the function hasOwnProperty on myObj, and feeds checkProp as an argument. Under the hood, hasOwnProperty iterates through the properties of myObj and compares them to the argument which is checkProp. It returns true if myObj contains checkProp, false if it does not.

Normally functions are structured like this : function hasOwnProperty(myObj, checkProp){  
… things inside this function …  
}  
However, since hasOwnProperty is a method, which means that it is a function stored in an object, it is accessed through dot notation. To be clear, myObj, actually contains the function hasOwnProperty as a property in it, and it’s value is the function that hasOwnProperty runs. You just can’t see it, but it’s there. So that’s why hasOwnProperty is invoked this way : myObj.hasOwnProperty(argument);

---

<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 14, 2016, 9:47pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/6 "2016-07-14T21:47:14Z")

</div>

Thank you P1xt and sethcoch. I’m going to need to spend some time looking at both of you wrote and trying to wrap my head around it. Appreciate your input.

---

<div class="post-metadata">

**Author:** ![tourdejord](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@tourdejord](https://forum.freecodecamp.org/u/tourdejord)\
**Post date:** [August 21, 2016, 9:31pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/7 "2016-08-21T21:31:50Z")

</div>

Thank you, One of your very similar inputs from a last lesson got me through it. Your hints are usefull in not only solving the problem, but also helps me retain what I’ve learned.

---

<div class="post-metadata">

**Author:** ![tourdejord](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@tourdejord](https://forum.freecodecamp.org/u/tourdejord)\
**Post date:** [August 21, 2016, 10:37pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/9 "2016-08-21T22:37:17Z")

</div>

no problem. What I found finally was that I was using dot notation instead of bracket notation to find the value of myObj. It’s important for users at this point of FCC to understand the difference.

---

<div class="post-metadata">

**Author:** ![marjanka3009](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@marjanka3009](https://forum.freecodecamp.org/u/marjanka3009)\
**Post date:** [January 11, 2017, 9:49am UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/10 "2017-01-11T09:49:55Z")

</div>

Pfew! I thought I was the only one struggling with these concepts. 🙂

---

<div class="post-metadata">

**Author:** ![BBSL1234](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@BBSL1234](https://forum.freecodecamp.org/u/BBSL1234)\
**Post date:** [August 25, 2019, 3:18am UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/11 "2019-08-25T03:18:31Z")

</div>

But what _is_ the difference???

I wish people would spell things out instead of making cryptic references.

The only time to prefer bracket notation is when the property is more than one word…and I guess when a string is passed into it, as in this “Testing Objects for Properties” exercise.

---

<div class="post-metadata">

**Author:** ![BBSL1234](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@BBSL1234](https://forum.freecodecamp.org/u/BBSL1234)\
**Post date:** [August 25, 2019, 3:39am UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/13 "2019-08-25T03:39:18Z")

</div>

**EDIT:** Okay, I see what you’re getting at now…thanks.

[[Album] JS Bracket Notation Safest to Use ![](https://i.imgur.com/3ytF9FA.jpg?fb "JS Bracket Notation Safest to Use") ](https://imgur.com/a/KghN1R8)

 ![fcc](https://global.discourse-cdn.com/freecodecamp/original/3X/b/a/bab1b1ef48cd411b39a4e80f9fff983719cf111b.png)

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
Not sure what you mean…but can you check out my [“Epiphany from ‘Testing Objects for Properties’?”](http://forum.freecodecamp.org/t/epiphany-from-testing-objects-for-properties/308905) Post?

It seems like in this Challenge, bracket notation is preferred because strings get passed into functions along with their quotations marks, thus rendering `myObj.checkProp` into `myObj."gift"`???

Whereas `myObj[checkProp]` conveniently gets rendered into `myObj["gift"]`…is that why?? Why bracket notation is required here?

---

<div class="post-metadata">

**Author:** ![BBSL1234](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@BBSL1234](https://forum.freecodecamp.org/u/BBSL1234)\
**Post date:** [August 25, 2019, 4:19am UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/15 "2019-08-25T04:19:05Z")

</div>

Thanks – I get it now. The way the FCC lessons are currently written up, it makes it seem like dot and bracket notations are exactly equal in their “powers” (i.e., in their application).

Seems like a few lessons are missing! I was wondering why there was such redundancy in the language. Turns out, it’s not a redundancy at all!

---

<div class="post-metadata">

**Author:** ![BBSL1234](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@BBSL1234](https://forum.freecodecamp.org/u/BBSL1234)\
**Post date:** [August 25, 2019, 7:07am UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/17 "2019-08-25T07:07:35Z")

</div>

Yeah this reminds me a bit of `==` versus `===`…so I think I’ll just stick to bracket notation. Except when calling properties like `length` – `array[length]` looks horrible!!

---

<div class="post-metadata">

**Author:** ![BBSL1234](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@BBSL1234](https://forum.freecodecamp.org/u/BBSL1234)\
**Post date:** [August 25, 2019, 7:11am UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/18 "2019-08-25T07:11:31Z")

</div>

> Dot notation can only be used for property names which do not have spaces and that are not referenced as variables.

Hmmm…but as you can see from [JS Bracket Notation Safest to Use - Album on Imgur](https://imgur.com/a/KghN1R8), `myObj.12` isn’t possible even though it’s not being “referenced as a variable.”

---

<div class="post-metadata">

**Author:** ![BBSL1234](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@BBSL1234](https://forum.freecodecamp.org/u/BBSL1234)\
**Post date:** [August 27, 2019, 8:12am UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/20 "2019-08-27T08:12:51Z")

</div>

Well `12` is the property name, after all, in

```auto
var testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
}

```

Seems a bit “unfair” that `12` is not considered valid for purposes of dot-notation but I’ll just file this under “Treat As If Separated by Spaces” (i.e., use bracket notation – “just the way it is”).

---

<div class="post-metadata">

**Author:** ![xorezx](https://avatars.discourse-cdn.com/v4/letter/x/5f8ce5/32.png) [@xorezx](https://forum.freecodecamp.org/u/xorezx)\
**Post date:** [January 8, 2020, 7:41pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/22 "2020-01-08T19:41:06Z")

</div>

That’s exactly the impression that I had too. It’s definitely not clear.

---

<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 25, 2021, 12:19pm UTC](https://forum.freecodecamp.org/t/im-stuck-on-testing-objects-for-properties/19710/23 "2021-01-25T12:19:28Z")

</div>


