JavaScript comparison operators are used to compare two values and return a boolean value (true or false) based on the comparison. These operators are essential in controlling the flow of a program, especially in conditional statements like if, while, and for loops.
1. Equality (==
) Operator
Compares two values for equality after converting them to a common type (type coercion).
Returns true if the values are equal, otherwise false
10 == "10"; // true (number 10 is coerced to string "10")
20 == 20 // true (number 20 is coerced to number 20)
2. Strict Equality (===
)
Compares two values for equality without performing type conversion. Both value and type must be the same.
10 === "10"; // false
10 === 10; // true
3. Inequality (!=
)
Compares two values for inequality without performing type conversion.
10 !== "10"; // true
10 !== 10; // false
4. Strict Inequality (!==
)
Compares two values for inequality without performing type conversion.
10 !== "10"; // true
10 !== 10; // false
5. Greater Than (>
)
Returns true if the left operand is greater than the right operand.
10 > 5; // true
6. Greater Than or Equal To (>=
)
Returns true if the left operand is greater than or equal to the right operand.
10 >= 10; // true
7. Less Than (<
)
Returns true if the left operand is less than the right operand.
5 < 10; // true
8. Less Than or Equal To (<=
)
Returns true if the left operand is less than or equal to the right operand.
5 <= 5; // true
9. Ternary Operator (? :
)
A shorthand for an if-else
statement. It takes three operands: a condition, a value if true, and a value if false.
const result = (10 > 5) ? "Yes" : "No"; // "Yes"