Skip to content

JavaScript comparison operators | Logical Operators

  • by

JavaScript Comparison operators are used to compare two values and give back a boolean value either true or false. used in logical statements (if else) to determine equality or difference between variables or values.

operand1 operator operand2
OperatorDescriptionExample
==Equal to5==5; //true
!=Not equal to5!=5; //false
===Strict equal to5==='5'; //false
!==Strict not equal to5!=='5'; //true
>Greater than3>2; //true
>=Greater than or equal to3>=3; //true
<Less than3<2; //false
<=Less than or equal to2<=2; //true

These comparison operators can be used to compare various types of values, such as numbers, strings, and objects, and they return a boolean value (true or false) based on the result of the comparison.

JavaScript comparison operators

Simple example code equal to the operator and Not Equal to the Operator.

<!DOCTYPE html>
<html>
<body>

  <script>

    const a = 5, b = 2, c = 'hello';

    // equal to operator
    console.log(a == 5);     // true
    console.log(b == '2');   // true
    console.log(c == 'Hello');  // false

    // not equal operator
    console.log(a != 2); // true
    console.log(b != 'Hello'); // true
</script>

</body>
</html> 

Output:

JavaScript comparison operators

Greater than Operator and Less than Operator

const a = 3;

// greater than operator
console.log(a > 2); // true

// less than operator
console.log(a < 2); // false

Logical Operators

Logical operators are used to determine the logic between variables or values.

If x = 6 and y = 3

OperatorDescriptionExample
&&Logical AND: true if both the operands/boolean values are true, else evaluates to false(x < 10 && y > 1) is true
||Logical OR: true if either of the operands/boolean values is true. evaluates to false if both are false(x == 5 || y == 5) is false
!Logical NOT: true if the operand is false and vice-versa.!(x == y) is true

Do comment if you have any doubts or suggestions on this JS operator topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading