JavaScript provides several comparison operators that allow you to compare values. Here are the main ones:
-
Equal to (
==): Compares two values for equality, performing type coercion if necessary.console.log(5 == '5'); // true -
Not equal to (
!=): Compares two values for inequality, performing type coercion.console.log(5 != '6'); // true -
Strict equal to (
===): Compares two values for equality without type coercion (both value and type must match).console.log(5 === 5); // true -
Strict not equal to (
!==): Compares two values for inequality without type coercion.console.log(5 !== '5'); // true -
Greater than (
>): Checks if the left value is greater than the right value.console.log(5 > 3); // true -
Less than (
<): Checks if the left value is less than the right value.console.log(3 < 5); // true -
Greater than or equal to (
>=): Checks if the left value is greater than or equal to the right value.console.log(5 >= 5); // true -
Less than or equal to (
<=): Checks if the left value is less than or equal to the right value.console.log(3 <= 5); // true
These operators are fundamental for making decisions in your code, such as in conditional statements and loops.
