Understand Own Properties - what does property do

Tell us what’s happening:
I dont understand what is happening in this code. I understand what an object is and why we would run this code(i think). What i dont understand is how its working?

What does the property part of the code do?
Is .hasOwnProperty built into JS?

Your code so far

function Bird(name) {
  this.name = name;
  this.numLegs = 2;

let canary = new Bird("Tweety");
let ownProps = [];
// Add your code below this line

for (let property in canary) {
  if (canary.hasOwnProperty(property)) {

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

Link to the challenge:

Hi! Welcome to the freeCodeCamp forum (saw this was your first post :slight_smile:)

Is .hasOwnProperty built into JS?

Yes, .hasOwnProperty() is built into JS; it’s a method that comes from Object.

For more info, checkout the doc on MDN.

What does the property part of the code do?

So if you know what a JavaScript object is, then you’re likely to also know they are made up of properties that hold pieces of data.

In this code snippet, per the for loop, property is being used to iterate through canary, a Bird object (you can iterate through the properties of an object).

Hopefully this makes things a little clearer?


Ok so i get what a property is in objects, but when we iterate through canary, what if we changed property to say pizza or some other word. would that make a difference?

So in this case, property is the variable that represents a given property in the object ( canary ) you’re iterating through in the for loop.

During each iteration, your code is pretty much doing this:

Iteration #1 of canary

if(canary.hasOwnProperty("name") {
    ownProps.push("name"); // ownProps = ["name"]

Iteration #2

if(canary.hasOwnProperty("numLegs") {
   ownProps.push("numLegs"); // ownProps = ["name", "numLegs"]

If you tried the following with your code as-is:


That would return false because “pizza” hasn’t been added to your original Bird object.