Difference between && and || and their opposites

Tell us what’s happening:
Hi, I copy-pasted Solution 1 provided in the Challenge Guide. I can’t understand why the code doesn’t work if I replace the || symbols in line 7 with &&. I also want to know what’s the opposite of:
if (!obj.hasOwnProperty(sourceKeys[i]) ||
obj[sourceKeys[i]] !== source[sourceKeys[i]])
It seems to me that it should be the following but it doesn’t work:
if(obj.hasOwnProperty(sourceKeys[i]) &&
obj[sourceKeys[i]] === source[sourceKeys[i]])

  **Your code so far**

function whatIsInAName(collection, source) {
let sourceKeys = Object.keys(source);
// Only change code below this line

return collection.filter(actor => {
  for (let i = 0; i < sourceKeys.length; i++) {
    if (!actor.hasOwnProperty(sourceKeys[i]) || actor[sourceKeys[i]] !== source[sourceKeys[i]]) {
      return false;
      }
    }
    return true;
});
}

// Only change code above this line

console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36

Challenge: Wherefore art thou

Link to the challenge:

|| means “or”. It evaluates to true if the expression on either side is truthy.
&& means “and”. It evaluates to false if the expression on either side is falsy.

If the expression on the left of && is falsy, it will immediately return false without checking the expression on the right. If the expression on the left of || is truthy, it will immediately return true without checking the expression on the right. (This is called “short circuiting”.)

If I understand the question correctly, then what you are looking for is the ! (“not”) operator.
a || b means “either a or b is truthy”.
!(a || b) means “both a and b are falsy”. It could also be written as !a && !b.
a && b means “both a and b are truthy”.
!(a && b) means “either a or b is falsy”. It could also be written as !a || !b.

1 Like

If you are using || the compiler checks left and right from this operator and it enough if min ONE side return true. If you are using a && operator BOTH sides have to be true (or false). By the way this concepts is also valid If you do have a chain of || or &&. It always look at left and right of the operator only and doesn’t check the complete chain at once.

a || b = a OR b
a && b = a AND b

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.