Comparisons
Comparisons
π¨βπΌ Our product system needs to compare valuesβprices, quantities, and more.
Understanding comparison operators is essential for making decisions in code.
Equality Operators
JavaScript has two types of equality:
Loose equality (
==) - Compares values after type conversion:100 == '100' // true (string converted to number)
0 == false // true (false converted to 0)
Strict equality (
===) - Compares values AND types:100 === '100' // false (number vs string)
0 === false // false (number vs boolean)
Always prefer
=== and !== (strict equality). Loose equality's type
conversion can cause subtle bugs that are hard to track down.Inequality Operators
The same distinction applies to "not equal":
!=- Loose inequality (with type conversion)!==- Strict inequality (no type conversion)
Relational Operators
These compare order/magnitude:
a > b // greater than
a < b // less than
a >= b // greater than or equal
a <= b // less than or equal
π¨ Open
and:
- Compare
price(number) andquantity(string) with both==and=== - Use
!=and!==to check ifais not equal tob - Use
>and<=to compareaandb


