JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Comparison and Logical Operators

8 April 2025 | Category:

JavaScript provides comparison operators to compare values and logical operators to combine multiple conditions.

These are widely used in if statements, loops, and any kind of decision-making logic.


🔍 Comparison Operators

Comparison operators compare two values and return a Boolean (true or false).

OperatorDescriptionExampleResult
==Equal to5 == "5"true
===Equal value and type5 === "5"false
!=Not equal to5 != 3true
!==Not equal value or type5 !== "5"true
>Greater than10 > 5true
<Less than10 < 5false
>=Greater than or equal to10 >= 10true
<=Less than or equal to5 <= 3false

Example:

console.log(7 == "7");   // true
console.log(7 === "7");  // false
console.log(5 > 3);      // true
console.log(5 < 3);      // false

đź”— Logical Operators

Logical operators are used to combine multiple Boolean conditions.

OperatorNameDescription
&&ANDTrue if both conditions are true
||ORTrue if at least one is true
!NOTReverses the condition (true → false)

Examples:

let age = 20;

console.log(age > 18 && age < 25);  // true (both true)
console.log(age > 25 || age < 30);  // true (one is true)
console.log(!(age > 18));           // false (negates true)

đź§Ş Real-World Example

let isLoggedIn = true;
let hasPermission = false;

if (isLoggedIn && hasPermission) {
  console.log("Access granted");
} else {
  console.log("Access denied");
}

Output:

Access denied

⚠️ Equality vs Strict Equality

  • == allows type conversion
  • === checks both value and type (more strict)
5 == "5"   // true
5 === "5"  // false

đź§  Summary

  • Comparison operators compare values and return Boolean.
  • Logical operators combine conditions.
  • Use === for safer comparisons (no type conversion).
  • &&, ||, and ! are key tools for decision-making.