Logical Operators

Logical Operators

  • Evaluates between two conditions/expressions and returns true or false

  • with && (Logical And) operators, both conditions have to be true in order for the whole expression to be true

  • with || (Logical Or) operators, any of the condition must be true for entire expression to be true; but if all conditions are false then the entire expression is false

Operator Description Example (all return true)
Logical AND (&&) expr1 && expr2 (3 > 1) && “cat”.length === 3
Logical OR (||) expr1 || expr2 (4 + 1) < 8 || 53 > (9 * 6)

Examples

JS Bin on jsbin.com

Exercise

Add the following expressions in the jsbin below and evaluate the result; use console.log() to print the result. Don’t forget to press “run” after adding your code.

  1. true && false

  2. true && 8 > (3 * 3) && "hello"

  3. true && (9 / 3 > 5) || "Yolo!"

  4. false || "Apple" === "apple" || parseInt("12") === 12

JS Bin on jsbin.com