Logical Operators

Logical operators are &&, | |, and much more which helps us while taking a decision.

&& β†’ and , || β†’ or and ! β†’ not

const hasDriverLicense = true; // A
const hasGoodVision = true; // B

console.log(hasDriverLicense && hasGoodVision);
console.log(hasDriverLicense || hasGoodVision);

console.log(!hasDriverLicense);

OR Operator | |

The logical OR is meant to manipulate boolean values only. If any of its arguments are true, it returns true, otherwise it returns false.

res = a && b;

If an operand is not a boolean, it’s converted to a boolean for the evaluation.

const hasDriverLicense = true;
const hasGoodVision = true;

console.log(hasDriverLicense || hasGoodVision); // true 

For instance, the number 1 is treated as true, the number 0 as false:

if (1 || 0) { // works just like if( true || false )
 console.log('truthy!');
}

AND Operator &&

res = val1 && val2 && val3;