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
index.ts
and:
  1. Compare price (number) and quantity (string) with both == and ===
  2. Use != and !== to check if a is not equal to b
  3. Use > and <= to compare a and b

Please set the playground first

Loading "Comparisons"
Loading "Comparisons"
Login to get access to the exclusive discord channel.
Loading Discord Posts