JavaScript Comparison Operators
Comparison operators test the relationship between two values and always return a boolean, forming the basis of every decision your code makes.
Comparing values
A comparison operator looks at two operands and answers a yes-or-no question, returning true or false. Is one number bigger than another? Are two values equal? These booleans then drive if statements, loops, and ternary expressions. Because so much program logic depends on comparisons, understanding exactly how they behave is essential.
Strict versus loose equality
JavaScript offers two kinds of equality. Strict equality (===) returns true only when both the value and the type match, with no conversion. Loose equality (==) first converts the operands to a common type and then compares, which can produce surprising results. As a rule, prefer === and !== so your comparisons stay predictable and free of hidden type coercion.
Strict versus loose
<!DOCTYPE html>
<html>
<body>
<h2>Strict versus loose</h2>
<script>
console.log(5 === 5); // true same value and type
console.log(5 === "5"); // false different types
console.log(5 == "5"); // true string coerced to number
console.log(0 == false); // true both coerced, surprising
console.log(0 === false); // false different types
</script>
</body>
</html>Relational operators
The relational operators greater than, less than, greater than or equal, and less than or equal compare the magnitude of two values. They work naturally with numbers, and with strings they compare character by character using Unicode order, so uppercase letters sort before lowercase ones. Mixing types can trigger conversion, so keep the operands the same type when you can.
Ordering and range checks
<!DOCTYPE html>
<html>
<body>
<h2>Ordering and range checks</h2>
<script>
let age = 20;
console.log(age >= 18); // true eligible
console.log(age < 13); // false
console.log("apple" < "banana"); // true a comes before b
// Range check combines two comparisons
let inRange = age > 12 && age < 65;
console.log(inRange); // true
</script>
</body>
</html>