ANDORoperators.md - brainchildservices/curriculum GitHub Wiki
Slide 1
Logical AND (&&)
The logical AND (&&) operator (logical conjunction) for a set of operands is true if and only if all of its operands are true. It is typically used with Boolean (logical) values. When it is, it returns a Boolean value. However, the && operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.
const a = 3;
const b = -2;
console.log(a > 0 && b > 0);
// expected output: false
Syntax
expr1 && expr2
Slide 2
Examples
Using AND
The following code shows examples of the && (logical AND) operator.
a1 = true && true // t && t returns true
a2 = true && false // t && f returns false
a3 = false && true // f && t returns false
a4 = false && (3 == 4) // f && f returns false
a5 = 'Cat' && 'Dog' // t && t returns "Dog"
a6 = false && 'Cat' // f && t returns false
a7 = 'Cat' && false // t && f returns false
a8 = '' && false // f && f returns ""
a9 = false && '' // f && f returns false
Slide 3
Conversion rules for booleans
Converting AND to OR
The following operation involving booleans:
bCondition1 && bCondition2
is always equal to:
!(!bCondition1 || !bCondition2)
Converting OR to AND
The following operation involving booleans:
bCondition1 || bCondition2
is always equal to:
!(!bCondition1 && !bCondition2)