2. What is an Operator?

2.3. Logical Operators


          Logical operators are usually used with conditional operations and will return a Boolean value. However, if these operators are used with non-Boolean values, it may return a non-Boolean value. 

      • Logical AND (&&): For Boolean operands, it will return true if both operands are true; otherwise, it will return false. For non-Boolean operands, it will return the operand on the left if it can be converted to false; otherwise, it will return the operand on the right. 

Ex. true && true will return true while "cat" && "dog" will return "dog". 

      • Logical OR (||): For Boolean operands, it will return false if both operands are false; otherwise, it will return true. For non-Boolean operands, it will return the operand on the left if it can be converted to true; otherwise, it will return the operand on the right.

 Ex. false || false will return false while "cat" || "dog" will return "cat". 

      • Logical NOT(!): It returns true if the operand is evaluated to false; otherwise, returns false. 

Ex. !(5=='5') will return false.

         Always remember that an expression can be converted to false if it is evaluated to null, 0, NaN, the empty string (""), or undefined.

Sample code:



Output: