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);
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!');
}
&&
operator evaluates operands from left to right.false
, stops and returns the original value of that operand.res = val1 && val2 && val3;