In this tutorial, you will learn all about JavaScript comparison operators with the help of examples. In the previous tutorial, we have seen JavaScript arithmetic operators with the help of examples.
Before going through this article, we will know something about Operator.
Headings of Contents
What is Operator ?
Operator is a special symbol that perform operation on operands. Operands can be values and variables.
JavaScript Comparison Operators
JavaScript Comparison operators are used to compare two values and return a boolean value. Comparison operators are used for decision-making and loops.
JavaScript Comparison Operators
Operator | Name | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
=== | Strict equal to | x === y |
!== | Not strict equal to | x !== y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Equal to
Equal to ( == ) operator return true if the operands are equal.
var x = 10, y = 10;
console.log(x == y); // true
Not equal to
Not equal to ( != ) return true if both operands are not equal.
var x = 10, y = 12;
console.log(x != y); // true
Strict equal to
Strict equal to ( === ) return true if the both operands are equal to same type.
var x = 10;
console.log(x === 10); // true
console.log(x === '10'); // false
Strict not equal to
Strict not equal to ( !== ) return true if the both operands are strictly not equal.
var x = 10;
console.log(x !== 10); // false
console.log(x !== '10'); // true
Greater than
Greater than ( > ) operator return true if the left operand is greater than right operand.
var x = 10;
console.log(x > 6); // true
Less than
Greater than ( < ) operator return true if the left operand is less than right operand.
var x = 10;
console.log(x < 116); // true
Greater than or equal to
Greater than or equal to ( >= ) operator is used to return true if the left operator greater than or equal to the right operand.
var x = 10;
console.log(x >= 10); // true
Less than or equal to
Less than or equal to ( <= ) operator is used to return true if the left operator less than or equal to the right operand.
var x = 10;
console.log(x <= 100); // true
console.log(x <= 10); // true
Conclusion
In this article, you have learned all about comparison operators in JavaScript with the help of examples. JavaScript comparison operators are used to always compare two or more values and return boolean values, for example, true or false.
If you like this article, please share and keep visiting for further JavaScript tutorials.
Reference:- Click Here