DEV Community

Mohamed Ajmal
Mohamed Ajmal

Posted on

Comparison and Logical Operators in JS.

Comparison: Comparison operators in JavaScript are used to compare two values. They always return true or false.
A comparison operator compares its operands and returns a Boolean value based on whether the comparison is true.

< (Less than) Less than operator.

(Greater than) Greater than operator.

<= Less than or equal operator.

= Greater than or equal operator.

The result of evaluating an equality operator is always of type Boolean based on whether the comparison is true.

== Equality operator.

!= Inequality operator.

=== Strict equality operator.

!== Strict inequality operator.

Example:
let a = 10;
let b = 20;

console.log(a > b); // false
console.log(a < b); // true
console.log(a == 10); // true
console.log(a === "10"); // false
console.log(a != b); // true
console.log(a !== "10"); // true
console.log(b >= 20); // true
console.log(a <= 5); // false

Logical Operator: Logical operators in JavaScript are used to combine or manipulate Boolean values (true or false). They are mainly used in decision-making and conditions.

AND (&&) - AND Operator return the true Value only if both the conditions is true.

console.log(1 && 1); // 1 (truthy)
console.log(true && false); // false
console.log(5 > 2 && 10 > 5); // true

OR (||) - OR operator return the true value, if only at least one condition is true.

console.log(0 || 1); // 1 (truthy)
console.log(true || false); // true
console.log(5 > 10 || 10 > 5); // true

NOT (!) - Not operator reverse the value.

console.log(!true); // false
console.log(!false); // true
console.log(!1); // false (because 1 is truth)
console.log(!0); // true (because 0 is false)

Ref : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators

Top comments (0)