Why am I only getting back the results of my final 'hasOwnProperty' call?

38 Views Asked by At

I am just wondering why when I call the 'hasOwnProperty' method multiple times, I am only being returned one boolean value in the console? It is always the final call that returns. The rest of my code is fully functional and if I switch round the order I call to check on where the 3 properties are it returns whichever call came last.

spot.hasOwnProperty("sit");
spot.hasOwnProperty("name");
spot.hasOwnProperty("species"); 

Cheers guys.

2

There are 2 best solutions below

0
On BEST ANSWER

They all return but the console just displays output of the latest command; You can put them in an array to see all responses at once

[spot.hasOwnProperty('sit'), spot.hasOwnProperty('name')]
0
On

Lacking context, I'm assuming this comes down to just Boolean logic. If you check your actions one at a time you will get the correct value.

var spot = {};
spot.sit = true;
//spot.name = "Spot";
spot.species = "dog";

console.log(spot.hasOwnProperty('sit'));
console.log(spot.hasOwnProperty('name'));
console.log(spot.hasOwnProperty('species'));

There are 2 options if you are checking all values at once: Boolean AND (&&) or Boolean OR (||).

    var spot = {};
    //spot.sit = true;
    spot.name = "Spot";
    spot.species = "dog";
    
    // Boolean OR
    console.log(spot.hasOwnProperty('sit') || spot.hasOwnProperty('name') || spot.hasOwnProperty('species'));
    
    // Boolean AND
    console.log(spot.hasOwnProperty('sit') && spot.hasOwnProperty('name') && spot.hasOwnProperty('species'));