If (beagle.hasOwnProperty(prop)) {confusion;}

Tell us what’s happening:
the if condition here is a little confusing to me , I understand that the .hasOwnProperty() method returns a boolean depending on whether the property was inherited or not.

In the if condition is it implied that we want a True boolean return from the method for the ownProps.push(prop); snippet of code to execute ??

Your code so far


function Dog(name) {
this.name = name;
}

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

let ownProps = [];
let prototypeProps = [];

// Only change code below this line

for (let prop in beagle) { 
if (beagle.hasOwnProperty(prop)) { 
  ownProps.push(prop);
} else { 
  prototypeProps.push(prop);
}
}


Your browser information:

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

Challenge: Iterate Over All Properties

Link to the challenge:

It returns a boolean if the object has it’s own property…
It’ll return false for the prototype properties

Blockquote The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

same thing , no ? if the property isn’t inherited, its the objects own.

Yes, absolutely right…

That’s not where my confusion is coming from, I said in my post that I understood that part. its the if condition and whether or not that the condition will execute based on an implied return of true of the hasOwnProperty() method. It seems that way to me but I would like a clear answer.

if the condition is true(If the property is not inherited), it’ll be pushed to the ownprops array (owned properties)
if it’s false then it’ll be pushed to the inherited properties array…
Hope it’s clear

Yea that is , but in the code itself its simply if (beagle.hasOwnProperty(prop)) there is no comparison to true or false and to be honest that does throw me off at times . I get hung up on things like this.

In Javascript each condition evaluates to a boolean value and thus (beagle.hasOwnProperty(prop)) is same as (beagle.hasOwnProperty(prop) == true), hence the code works either way.

Both the lines check if the property beagle is it’s own property or one of the prototype’s property and control the flow of if statement accordingly.

1 Like

I see so unless we specify that we want the condition to execute if the return is false , it will assume we want a return of true, correct?

See the structure is

if(true){
    //True condition;
else{
    //False condition
}

When we specify a==3 it will evaluate it true or false,
In the above case the function hasOwnProperty() is returning a boolean, therefore there’s no need to compare it with true or false because eventually true==true will always be true
Check this if still unclear…

That’s the the case. In javascript all truth values return (evaluate to) true except these falsy values which are Null, “”, NaN, undefined, 0. So,

let myVar = 5; 
if (myVar) {
   console.log("Value is true.");
} else {
  console.log("Value is false);
}

//Logs "Value is true"

If however you change the value of myVar to 0 or Null or any of the falsy values, it will evaluate to false.

Thus when the beagle has it’s own property the condition evaluates to true and if(true) will execute the code in truth block, else if it doesn’t have it’s own property, the condition (beagle.hasOwnProperty(prop)) will evaluate to false and if(false) will execute the else block.

2 Likes

In case if you don’t want condition to execute if the return is false, you will simply omit the else block. The above is simply an outcome of how truth and falsy values work in Javascript. Each return either true or false in background (when processed as a condition).

2 Likes

OK thank you , this is what I call a good answer to my question . now i honestly understand why in the condition we are not comparing anything . this was very helpful thank you again !!