JavaScript Comparison Operators 🔍
Comparison operators in JavaScript compare two values and return a Boolean (true
or false
).
1️⃣ Types of Comparison Operators
Operator | Name | Example | Returns |
---|---|---|---|
== | Equal to | 5 == "5" | true (loose equality) |
=== | Strict equal to | 5 === "5" | false (strict equality) |
!= | Not equal to | 5 != "5" | false |
!== | Strict not equal to | 5 !== "5" | true |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 5 | true |
>= | Greater than or equal to | 5 >= 5 | true |
<= | Less than or equal to | 4 <= 5 | true |
2️⃣ Loose vs. Strict Equality (==
vs. ===
)
🔹 ==
(Loose Equality): Converts data types before comparing.
🔹 ===
(Strict Equality): Does not convert types, checks both value & type.
✅ Always use ===
for accurate comparisons!
3️⃣ Not Equal (!=
vs. !==
)
!=
(loose inequality) converts types before comparison.!==
(strict inequality) does not convert types.
✅ Use !==
for accurate comparisons!
4️⃣ Comparing Numbers
5️⃣ Comparing Strings
🔹 JavaScript compares strings based on Unicode order (character-by-character).
🔹 Uppercase letters come before lowercase letters.
6️⃣ Comparing Different Data Types
- JavaScript converts non-numbers to numbers when comparing.
🚨 Avoid comparisons with null
and undefined
as they can behave unexpectedly!
🎯 Summary
✅ Use ===
and !==
instead of ==
and !=
✅ Be cautious when comparing different data types
✅ String comparisons use Unicode order
🚀 Now you're ready to compare values in JavaScript like a pro! Let me know if you need more details. 😊