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));
"Alan"&&"Jeff"&&"Sarah"&&"Ryan"

This evaluates to "Ryan"

image

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

2 Likes

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