Checking value with `.hasOwnProperty` lead to undesired return value

Continuing the discussion from freeCodeCamp Challenge Guide: Check if an Object has a Property

Here is another solution that should work but does not pass the tests.

function isEveryoneHere(userObj) {
  if(userObj.hasOwnProperty("Alan"&&"Jeff"&&"Sarah"&&"Ryan")){
    return true
  }else{
    return false
  }
}

console.log(isEveryoneHere(users));

It does not work because hasOwnProperty expects a String not a Boolean expression.

1 Like
"Alan"&&"Jeff"&&"Sarah"&&"Ryan"

This evaluates to "Ryan"

image

So you’re only checking if the userObj has a Ryan property.

2 Likes

Interesting… I would have thought it would have been true instead of Ryan.

Yeah, one of those JavaScript quirks.

&& evaluates to its final truthy value when all operands are truthy, otherwise its first falsy value,
|| evaluates to its first truthy value when one exists, otherwise its final falsy value.

1 Like