← Back to Chapters

JavaScript Comparison & Logical Operators

? JavaScript Comparison & Logical Operators

? Quick Overview

JavaScript comparison and logical operators are used to compare values and combine conditions. They always return a boolean value: true or false. These operators are heavily used in if statements, loops, and other control flow logic.

? Key Concepts

  • Comparison operators compare two values (numbers, strings, etc.).
  • They answer questions like “Is A equal to B?” or “Is A greater than B?”.
  • Logical operators combine or invert multiple boolean expressions.
  • They help build complex conditions using &&, ||, and !.
  • Strict comparison (===) checks both value and data type.
  • All of them finally evaluate to true or false.

? Syntax and Behavior

? Comparison Operators

Comparison operators compare two values and return a boolean result.

Operator Description Example Result
== Equal to (loose, allows type coercion) 5 == "5" true
=== Equal value and type (strict) 5 === "5" false
!= Not equal (loose) 5 != "5" false
!== Not equal value or type (strict) 5 !== "5" true
> Greater than 8 > 5 true
< Less than 3 < 5 true
>= Greater than or equal to 10 >= 10 true
<= Less than or equal to 4 <= 3 false

⚙️ Logical Operators

Logical operators combine boolean expressions to build complex conditions.

Operator Name Description Example
&& Logical AND Returns true if both conditions are true (x > 5 && y < 10)
|| Logical OR Returns true if at least one condition is true (x > 5 || y < 10)
! Logical NOT Reverses the result of a condition !(x > 5)

? Code Examples

? Comparison operators in action
console.log(5 == "5");    // true (values equal, types differ)
console.log(5 === "5");   // false (number vs string)
console.log(10 > 7);      // true
console.log(4 <= 3);      // false
? Logical operators with conditions
let a = 6;
let b = 3;

console.log(a > 5 && b < 5);  // true  (both conditions are true)
console.log(a < 5 || b < 2);  // false (both conditions are false)
console.log(!(a == b));       // true  (a == b is false, !false = true)

? Live Output and Explanation

? Comparison results

  • 5 == "5"true (values are equal, types are ignored).
  • 5 === "5"false (number is not the same type as string).
  • 10 > 7true (10 is greater than 7).
  • 4 <= 3false (4 is not less than or equal to 3).

? Logical evaluation

  • a > 5 && b < 5true because both parts are true.
  • a < 5 || b < 2false because both parts are false.
  • !(a == b)true because a == b is false, and NOT of false is true.

✅ Tips and Best Practices

  • Prefer === and !== over == and != to avoid unexpected type coercion.
  • Use logical operators to combine conditions inside if, while, and other control structures.
  • Remember short-circuit behaviour:
    • a || b skips evaluating b if a is already true.
    • a && b skips evaluating b if a is false.
  • Wrap complex expressions with parentheses to make them easier to read and less error-prone.
  • Be careful not to confuse assignment (=) with comparison (== / ===).

? Try It Yourself

  • Declare two variables and compare them using == and ===. Observe how the result changes when one is a number and the other is a string.
  • Write an if statement that uses && to check two conditions (for example, age range: age >= 18 && age <= 60).
  • Create a condition with || that prints a message if either of two conditions is true (for example, discount for students or senior citizens).
  • Use ! with an expression like !(score >= 50) and log both the original and negated results to understand how NOT works.