Comparison Operators
Comparison Operators
Section titled “Comparison Operators”Introduction
Section titled “Introduction”Comparison operators compare two values and return a boolean (true or false). They are fundamental to decision-making in code.
Visual Explanation
Section titled “Visual Explanation”flowchart TD A["5 === '5'"] --> B["false — strict equality"] C["5 == '5'"] --> D["true — loose equality"] E["5 > 3"] --> F["true — greater than"] G["5 < 3"] --> H["false — less than"]Strict vs Loose Equality
Section titled “Strict vs Loose Equality”// Strict (recommended) — checks value AND type5 === 5; // true5 === '5'; // false — different types5 !== '5'; // true
// Loose (avoid) — coerces types5 == '5'; // true — coercive comparison5 != '5'; // falseRelational Operators
Section titled “Relational Operators”5 > 3; // true5 >= 5; // true3 < 5; // true3 <= 5; // trueString Comparison
Section titled “String Comparison”'apple' < 'banana'; // true — lexicographic order'Apple' < 'apple'; // true — uppercase < lowercaseBest Practices
Section titled “Best Practices”- Always use
===and!==instead of==and!= - Be careful with floating-point comparisons
- Use
Object.is()forNaNcomparisons
Summary
Section titled “Summary”===checks value and type (strict)==checks value with coercion (loose)- Always prefer
=== - Relational operators work on numbers and strings