# Get list of properties/method from JS

**URL:** <https://forum.freecodecamp.org/t/get-list-of-properties-method-from-js/160340>\
**Category:** General\
**Created:** [November 22, 2017, 8:52am UTC](https://forum.freecodecamp.org/t/get-list-of-properties-method-from-js/160340 "2017-11-22T08:52:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FakhriAz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fakhriaz/32/64362_2.png) [@FakhriAz](https://forum.freecodecamp.org/u/FakhriAz)\
**Post date:** [November 22, 2017, 8:52am UTC](https://forum.freecodecamp.org/t/get-list-of-properties-method-from-js/160340/1 "2017-11-22T08:52:17Z")

</div>

I am studying JS and I got stuck in a chapter of _A Smarter Way to Learn JavaScript._

If you want a list of an object’s properties you do this as written in the book:

```auto
1 var listOfProperties = [];
2 for (var prop in plan1) {
3 listOfProperties.push(prop);
4 }

```

I could understand line 1 but for rest not really. If you could explain this to me in plain English with links for further reading I will be grateful.

---

<div class="post-metadata">

**Author:** ![JohnL3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnl3/32/16459_2.png) [@JohnL3](https://forum.freecodecamp.org/u/JohnL3)\
**Post date:** [November 22, 2017, 9:12am UTC](https://forum.freecodecamp.org/t/get-list-of-properties-method-from-js/160340/2 "2017-11-22T09:12:32Z")

</div>

Here is a link to a bit of code showing it being used … its done in pythontutor … as your code dosent have a object plan1 i created a small one with two properties to show it work.

[http://www.pythontutor.com/visualize.html#code=var%20plan1%20%3D%20{‘make’%3A%20’car’,%20’type’%3A%20’manual’}%3B var%20listOfProperties%20%3D%20[]%3B %20for%20(var%20prop%20in%20plan1)%20{ listOfProperties.push(prop)%3B } console.log(listOfProperties)&cumulative=false&curInstr=9&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=[]&textReferences=true](http://www.pythontutor.com/visualize.html#code=var%20plan1%20%3D%20%7B'make'%3A%20'car',%20'type'%3A%20'manual'%7D%3B%0Avar%20listOfProperties%20%3D%20%5B%5D%3B%0A%20for%20%28var%20prop%20in%20plan1%29%20%7B%0AlistOfProperties.push%28prop%29%3B%0A%7D%0Aconsole.log%28listOfProperties%29&cumulative=false&curInstr=9&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=%5B%5D&textReferences=true)

---

<div class="post-metadata">

**Author:** ![FakhriAz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fakhriaz/32/64362_2.png) [@FakhriAz](https://forum.freecodecamp.org/u/FakhriAz)\
**Post date:** [November 22, 2017, 9:54am UTC](https://forum.freecodecamp.org/t/get-list-of-properties-method-from-js/160340/3 "2017-11-22T09:54:44Z")

</div>

Thanks, but I could not grasp it yet.

Any idea about the use of **prop**?

---

<div class="post-metadata">

**Author:** ![stephanpssantos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/stephanpssantos/32/71766_2.png) [@stephanpssantos](https://forum.freecodecamp.org/u/stephanpssantos)\
**Post date:** [November 22, 2017, 12:10pm UTC](https://forum.freecodecamp.org/t/get-list-of-properties-method-from-js/160340/4 "2017-11-22T12:10:28Z")

</div>

If you’ve seen javascript objects before, you know they follow a certain form:  
`{property1: value1, property2: value2}`

Imagine this was the object called “plan 1” :  
`var plan1 = {apple: 1, banana: 2, coconut: 3};`  
The properties of the “plan1” object are apple, banana, and coconut.

A “for…in” loop is used specifically for objects and it loops through the object.

`for (var prop in plan1) {`  
The “for” and the “in” part should be clear. That’s just the syntax for a “for…in” loop.  
The “var prop” part just declares a new variable called “prop” that’s to be used specifically for the loop.  
You don’t have to call it prop, you can call it whatever you want.  
The “plan1” part is the actual object that you want to loop through.

The loop will run once for each item in the object, so going back to our “plan1” example,  
`var plan1 = {apple: 1, banana: 2, coconut: 3};`  
In this case, because the object has 3 items, the loop will run three times.

Each time it runs, the variable you declared (in our case, the variable is “prop”) will have it’s value changed to the property of whichever item the loop is processing. In other words, the first time the loop runs, the variable “prop” will equal “apple”, the second time prop will equal “banana”, and the third time, “prop” will equal “coconut”.

This part should now make sense:  
`listOfProperties.push(prop);`

It’s pushing each property name (“apple”, “banana”, and “coconut”) into the “listOfProperties” array.

At the end, the “listOfProperties” array should equal [“apple”, “banana”, “coconut”]

Hopefully that’s clear.

Here’s a resource on “for…in” loops:

> **[for...in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)**
>
> The for...in statement iterates over the enumerable properties of an object. For each distinct property, statements can be executed.

---

<div class="post-metadata">

**Author:** ![TomerPacific](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tomerpacific/32/158967_2.png) [@TomerPacific](https://forum.freecodecamp.org/u/TomerPacific)\
**Post date:** [November 22, 2017, 1:11pm UTC](https://forum.freecodecamp.org/t/get-list-of-properties-method-from-js/160340/5 "2017-11-22T13:11:41Z")

</div>

Hey @FakhriAz,  
what the second line does is iterate over the properties of the object named plan1 and adding it to the listOfProperties array.

To further explain, let’s say you have an object plan1 with the following properties:

```auto
let plan1 = {
     first : "nothing",
     second : "something",
     third : 3
};

```

Once you run the code you mentioned, the listOfProperties array will have the fields first, second and third since those are the properties of the plan1 object. You will NOT have the values of those properties.

Hope that clarifies things for you.

---

<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 17, 2021, 8:34pm UTC](https://forum.freecodecamp.org/t/get-list-of-properties-method-from-js/160340/6 "2021-01-17T20:34:02Z")

</div>

Closed on January 17, 2021, 8:34pm UTC


