JavaScript Comparison and Logical Operators


Operators are a core part of any programming language. They help developers make decisions in their code, compare values, and perform logical operations. In JavaScript, comparison and logical operators play a crucial role in decision-making and controlling the flow of code.


1. JavaScript Comparison Operators

Comparison operators are used to compare two values or expressions. These operators return a boolean value (true or false) based on the comparison result.

The Common Comparison Operators in JavaScript:

  • Equal to (==)
  • Strict Equal to (===)
  • Not equal to (!=)
  • Strict Not equal to (!==)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

Let’s dive into each of these comparison operators with examples.


1.1 Equal to (==)

The == operator compares two values for equality, but it performs type coercion. This means that JavaScript will convert the values to the same type before making the comparison.

console.log(5 == "5");  // Output: true (string "5" is converted to number 5)
console.log(0 == false); // Output: true (false is converted to 0)

1.2 Strict Equal to (===)

The === operator checks if two values are equal and of the same type. It does not perform type coercion.

console.log(5 === "5");  // Output: false (different types)
console.log(5 === 5);    // Output: true (same type and value)

1.3 Not Equal to (!=)

The != operator checks if two values are not equal, but it allows type coercion.

console.log(5 != "5");    // Output: false (converted to same type, so values are equal)
console.log(0 != false);  // Output: false (false is converted to 0)

1.4 Strict Not Equal to (!==)

The !== operator checks if two values are not equal or not of the same type. It does not perform type coercion.

console.log(5 !== "5");   // Output: true (different types)
console.log(5 !== 5);     // Output: false (same type and value)

1.5 Greater Than (>)

The > operator compares if the value on the left is greater than the value on the right.

console.log(10 > 5);  // Output: true
console.log(5 > 10);  // Output: false

1.6 Less Than (<)

The < operator compares if the value on the left is less than the value on the right.

console.log(3 < 7);  // Output: true
console.log(8 < 5);  // Output: false

1.7 Greater Than or Equal to (>=)

The >= operator checks if the value on the left is greater than or equal to the value on the right.

console.log(7 >= 5);  // Output: true
console.log(3 >= 5);  // Output: false

1.8 Less Than or Equal to (<=)

The <= operator checks if the value on the left is less than or equal to the value on the right.

console.log(5 <= 5);  // Output: true
console.log(6 <= 4);  // Output: false

2. JavaScript Logical Operators

Logical operators are used to combine multiple conditions in JavaScript. They allow developers to make more complex decisions in code. JavaScript provides three primary logical operators:

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)

Let's look at each operator with examples.


2.1 Logical AND (&&)

The && operator returns true if both operands are truthy. If any operand is falsy, it returns false.

console.log(true && true);   // Output: true
console.log(true && false);  // Output: false
console.log(false && true);  // Output: false
console.log(false && false); // Output: false

Example with values:

let x = 5;
let y = 10;

console.log(x > 2 && y < 15);  // Output: true (both conditions are true)
console.log(x > 10 && y < 15); // Output: false (first condition is false)

2.2 Logical OR (||)

The || operator returns true if any one of the operands is truthy. If both operands are falsy, it returns false.

console.log(true || false);   // Output: true
console.log(false || false);  // Output: false
console.log(true || true);    // Output: true
console.log(false || true);   // Output: true

Example with values:

let x = 5;
let y = 10;

console.log(x < 10 || y > 15);  // Output: true (first condition is true)
console.log(x > 10 || y > 15);  // Output: false (both conditions are false)

2.3 Logical NOT (!)

The ! operator is used to negate a boolean value. It returns false if the value is truthy, and true if the value is falsy.

console.log(!true);   // Output: false
console.log(!false);  // Output: true
console.log(!(5 > 3)); // Output: false (because 5 > 3 is true, and negating it gives false)

Example with values:

let x = 5;

console.log(!(x > 3));  // Output: false (because x > 3 is true, negating it gives false)
console.log(!(x < 3));  // Output: true (because x < 3 is false, negating it gives true)

3. Precedence of Comparison and Logical Operators

When combining comparison and logical operators in a single expression, it's important to understand operator precedence. The precedence determines the order in which operators are evaluated.

Operator Precedence:

  1. Comparison operators (==, ===, !=, !==, <, >, <=, >=) have higher precedence than logical operators (&&, ||, !).
  2. Logical NOT (!) has higher precedence than AND (&&) and OR (||).

Example:

let x = 5;
let y = 10;
let z = 15;

console.log(x < y && y < z);  // Output: true (evaluates x < y first, then y < z, and combines with AND)
console.log(x < y || y > z);  // Output: true (evaluates x < y first, then y > z, combines with OR)