Comparison Operators


Comparison Operators

Operators that compare values and return true or false are known as comparison operators. >, >=, =, ===, and!== are some of the operators. Operators that combine several boolean expressions or values into a single boolean output are known as logical operators.

When two values are compared, the comparison operator returns a boolean value: true or false. In decision-making and loops, comparison operators are employed.

Operator Description Example
== If the operands are equal, then true. 5==5; //true
!= If the operands are not equal, this is true. 5!=5; //false
=== "=== True if the operands are of the same type and are equal." True if the operands are of the same type and are strictly equal. 5==='5'; //false
!== True if the operands are equal but of different types, or if they are not equal at all. 5!=='5'; //true
> The condition is true if the left operand is greater than the right operand. 3>2; //true
>= The condition is true if the left operand is greater than or equal to the right operand.3>=3; //true
< If the left operand is less than the right operand, the condition is true. 3<2; //false
<= If the left operand is smaller than or equal to the right operand, the condition is true. 2<=2; //true

function test_Equal(num) { if (num == 15) { return "Equal"; } return "Not equal"; }
console.log(test_Equal('15')); // "Equal" console.log(test_Equal(15)); // "Equal"
console.log(test_Equal(25)); // "Not equal"

Example:

<html> <body> <h2>Knowledge2life</h2> <input id="age" value="12" /> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { let age = document.getElementById("age").value; let voteable = (age < 18) ? "Too young":"Old enough"; document.getElementById("demo").innerHTML = voteable + " to vote."; } </script> </body> </html>

OUTPUT:

Knowledge2life