JavaScript Logical Operations

  • This lesson covers logical operators used in JavaScript.
  • Introduction to Logical Operations

    In JavaScript, logical operators are used to combine or evaluate multiple conditions.

    The main logical operators are:

    • && (Logical AND)

    • || (Logical OR)

    • ! (Logical NOT)

    Among these, AND (&&) and OR (||) follow a special behavior called short-circuit evaluation.

    What is Short-Circuit Evaluation ?

    Short-circuit evaluation means:

    JavaScript stops evaluating an expression as soon as the final result is already determined.

    In simple words:

    • JavaScript does not check unnecessary conditions

    • It saves time and avoids errors

    • It improves performance

      Why is it Called “Short-Circuit” ?

      Because the evaluation process is cut short when the result is known early.

      Short-Circuit in Logical AND (&&)

      Rule of && Operator

      • If the first condition is false, JavaScript does not check the second condition

      • Because for AND to be true, all conditions must be true

      Truth Table (AND)

      Condition 1Condition 2Result
      truetruetrue
      truefalsefalse
      falsetruefalse
      falsefalsefalse

    AND Short-Circuit

    Returns false immediately when the first value is false.

    // AND short-circuit
    
    let a = false;
    let b = true;
    
    console.log(a && b); // false
    • Explanation

      • a is false

      • AND operation already fails

      • b is never evaluated

    Safe Property Access with AND

    Prevents errors by checking if object exists before accessing its property.

    // Safe access using &&
    
    let user = null;
    
    user && console.log(user.name);
    • Why This Is Useful

      • Prevents error

      • Avoids accessing property of null

      • Second part runs only if user exists

        Why This Is Useful

        • Prevents error

        • Avoids accessing property of null

        • Second part runs only if user exists

        Short-Circuit in Logical OR (||)

        Rule of || Operator

        • If the first condition is true, JavaScript does not check the second condition

        • Because for OR to be true, only one condition must be true

        Truth Table (OR)

        Condition 1Condition 2Result
        truetruetrue
        truefalsetrue
        falsetruetrue
        falsefalsefalse

      OR Short-Circuit

      Returns true immediately when the first value is true.

      // OR short-circuit
      
      let x = true;
      let y = false;
      
      console.log(x || y); // true
      • Explanation

        • x is true

        • Result is already true

        • y is never evaluated

      OR Short-Circuit for Default Value

      Uses OR to fall back to a default when the first value is empty.

      // Default value using OR
      
      let username = "";
      
      let displayName = username || "Guest";
      
      console.log(displayName); // Guest
      • Explanation

        • Empty string is falsy

        • JavaScript uses fallback value

          Short-Circuit with Boolean and Non-Boolean Values

          JavaScript logical operators return actual values, not just true or false.

          AND (&&) Returns

          • First falsy value

          • Or last truthy value

          console.log(0 && "Hello");      // 0

          console.log("Hi" && "Hello");  // Hello

          OR (||) Returns

          • First truthy value

          • Or last falsy value

          console.log("Hi" || "Hello");  // Hi

          console.log("" || "Default");  // Default

          Common Falsy Values in JavaScript

          • false

          • 0

          • " " (empty string)

          • null

          • undefined

          • NaN

          These values affect short-circuit behavior.

          Short-Circuit vs Full Evaluation

          FeatureShort-CircuitFull Evaluation
          Evaluation stops earlyYesNo
          PerformanceBetterSlower
          Error preventionYesNo
          Used in JS logical opsYesNo

          Common Mistakes

          • Expecting both conditions to run

          • Forgetting falsy values

          • Misusing || for default when 0 is valid

          • Not understanding returned values

          Best Practices

          • Use short-circuiting to avoid errors

          • Use && for conditional execution

          • Use || carefully for default values

          • Write readable logical expressions

          Analogy

          • AND (&&):
            Entry allowed only if ID card and permission are present

          • OR (||):
            Entry allowed if ID card or permission is present

          If the decision is already clear, no further checking is needed.