In JavaScript, comparison operators are used to compare two values. The result of a comparison is a boolean value: true
or false
.
Here are the most common comparison operators in JavaScript:
==
===
!=
!==
>
>=
<
<=
// Example comparisons let x = 5; let y = "5"; console.log(x == y); // true (loose equality) console.log(x === y); // false (strict equality) console.log(x != y); // false console.log(x !== y); // true console.log(x > 3); // true console.log(x < 10); // true
In JavaScript, the strict equality operator (===
) is recommended over the loose equality operator (==
) because it compares both the value and the type. The loose equality operator may produce unexpected results due to type coercion.
Example:
// Loose equality example: console.log(5 == "5"); // true (coerces string to number) // Strict equality example: console.log(5 === "5"); // false (different types)
===
to avoid unexpected results due to type coercion.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!