Comparison operators allow you to compare two values and return a true
or false
result. You can then use the result to make a decision or control the flow of your program. You use comparisons in if
statements, loops, and many other situations where you need to make decisions based on certain conditions. Let’s dive into the most common comparison operators and see how they work.
The greater than operator, represented by a right-angle bracket (>
), checks if the value on the left is greater than the one on the right:
let a = 6;
let b = 9;
console.log(a > b); // false
console.log(b > a); // true
The greater than or equal operator, represented by a right-angle bracket and the equals sign (>=
), checks if the value on the left is either greater than or equal to the one on the right:
let a = 6;
let b = 9;
let c = 6;
console.log(a >= b); // false
console.log(b >= a); // true
console.log(a >= c); // true
The lesser than operator, represented by a left-angle bracket (<
) works similarly to >
, but in reverse. It checks if the value on the left is smaller than the one on the right:
let a = 6;
let b = 9;
console.log(a < b); // true
console.log(b < a); // false
The less than or equal operator, represented by a left-angle bracket and the equals sign (<=
) checks if the value on the left is smaller than or equal to the one on the right:
let a = 6;
let b = 9;
let c = 6;
console.log(a <= b); // true
console.log(b <= a); // false
console.log(a <= c); // true