Bird
Raised Fist0
Javascriptprogramming~5 mins

Logical operators in Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the logical AND operator (&&) do in JavaScript?
The logical AND operator (&&) returns true if both values on its sides are true. If either side is false, it returns false.
Click to reveal answer
beginner
What is the result of true || false in JavaScript?
The result is true because the logical OR operator (||) returns true if at least one side is true.
Click to reveal answer
beginner
Explain the logical NOT operator (!) in JavaScript.
The logical NOT operator (!) reverses the truth value. If the value is true, it becomes false; if false, it becomes true.
Click to reveal answer
intermediate
What will false && (anything) evaluate to and why?
It will evaluate to false immediately because with AND, if the first value is false, the whole expression is false without checking the second value.
Click to reveal answer
intermediate
How does short-circuit evaluation work with logical OR (||)?
With OR (||), if the first value is true, JavaScript does not check the second value because the whole expression will be true anyway.
Click to reveal answer
What is the result of true && false?
Atrue
Bfalse
Cundefined
Dnull
Which operator returns true if at least one operand is true?
A||
B!
C&&
D===
What does the expression !false evaluate to?
Atrue
Bfalse
Cnull
Dundefined
In false && someFunction(), will someFunction() be called?
AOnly if someFunction returns false
BYes, always
COnly if someFunction returns true
DNo, because of short-circuit
What is the result of true || false?
Anull
Bfalse
Ctrue
Dundefined
Explain how the logical AND (&&) and OR (||) operators work in JavaScript, including short-circuit behavior.
Think about when JavaScript stops checking the second value.
You got /3 concepts.
    Describe the effect of the logical NOT (!) operator on a boolean value.
    It flips the truth value.
    You got /3 concepts.

      Practice

      (1/5)
      1. Which logical operator in JavaScript returns true only if both conditions are true?
      easy
      A. ! (NOT)
      B. || (OR)
      C. && (AND)
      D. ? (Ternary)

      Solution

      1. Step 1: Understand AND operator behavior

        The AND operator (&&) returns true only when both conditions it connects are true.
      2. Step 2: Compare with other operators

        The OR operator (||) returns true if at least one condition is true, NOT (!) flips true/false, and ? is a ternary operator, not a logical operator.
      3. Final Answer:

        && (AND) -> Option C
      4. Quick Check:

        AND = && [OK]
      Hint: AND needs both true, so look for && [OK]
      Common Mistakes:
      • Confusing OR (||) with AND (&&)
      • Thinking NOT (!) combines conditions
      • Mixing ternary operator with logical operators
      2. Which of the following is the correct syntax to negate a boolean variable isActive in JavaScript?
      easy
      A. not isActive
      B. !isActive
      C. isActive!
      D. isActive.not()

      Solution

      1. Step 1: Recall NOT operator syntax

        In JavaScript, the NOT operator is a single exclamation mark (!) placed before the variable to flip its boolean value.
      2. Step 2: Check each option

        !isActive is the correct syntax. Options A, C, and D are invalid syntax in JavaScript.
      3. Final Answer:

        !isActive -> Option B
      4. Quick Check:

        NOT = ! before variable [OK]
      Hint: Use ! before variable to negate boolean [OK]
      Common Mistakes:
      • Writing not instead of !
      • Placing ! after variable
      • Using method calls like .not()
      3. What is the output of this code?
      const a = true;
      const b = false;
      console.log(a || b && !b);
      medium
      A. true
      B. undefined
      C. false
      D. SyntaxError

      Solution

      1. Step 1: Evaluate NOT operator first

        !b means !false which is true.
      2. Step 2: Evaluate AND operator next

        b && !b is false && true which is false.
      3. Step 3: Evaluate OR operator last

        a || (result of AND) is true || false which is true.
      4. Final Answer:

        true -> Option A
      5. Quick Check:

        Operator precedence: ! > && > || = true [OK]
      Hint: Remember ! before && before || precedence [OK]
      Common Mistakes:
      • Ignoring operator precedence
      • Evaluating OR before AND
      • Misunderstanding ! operator
      4. Find the error in this code snippet:
      let x = true;
      if (x && !x) {
      console.log('Yes');
      } else {
      console.log('No');
      }
      medium
      A. The condition x && !x is always false
      B. Missing semicolon after if statement
      C. Cannot use ! operator on boolean
      D. Console.log syntax is wrong

      Solution

      1. Step 1: Analyze the condition x && !x

        x is true, !x is false, so true && false is always false.
      2. Step 2: Understand effect on code flow

        Since condition is always false, 'Yes' will never print, only 'No' will print.
      3. Final Answer:

        The condition x && !x is always false -> Option A
      4. Quick Check:

        Contradictory condition = always false [OK]
      Hint: Check if condition can ever be true [OK]
      Common Mistakes:
      • Thinking !x is invalid on boolean
      • Expecting 'Yes' to print
      • Confusing syntax errors with logic errors
      5. You want to check if a user is logged in and has admin rights before showing admin panel. Which condition correctly uses logical operators?
      const loggedIn = true;
      const isAdmin = false;
      // Choose the correct condition to show admin panel
      hard
      A. if (loggedIn || isAdmin) { showAdminPanel(); }
      B. if (!loggedIn || !isAdmin) { showAdminPanel(); }
      C. if (!loggedIn && isAdmin) { showAdminPanel(); }
      D. if (loggedIn && isAdmin) { showAdminPanel(); }

      Solution

      1. Step 1: Understand requirement

        Both loggedIn and isAdmin must be true to show admin panel.
      2. Step 2: Check each condition

        The condition with || (OR) is true if at least one is true. The condition with !loggedIn || !isAdmin is true unless both are true. The condition with !loggedIn && isAdmin is true only if not logged in and is admin. The condition with && (AND) requires both true.
      3. Final Answer:

        if (loggedIn && isAdmin) { showAdminPanel(); } -> Option D
      4. Quick Check:

        Both true needed = use AND (&&) [OK]
      Hint: Use && when all conditions must be true [OK]
      Common Mistakes:
      • Using OR (||) when both conditions are required
      • Negating conditions incorrectly
      • Confusing logic for access control
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard