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
. The starter provides:
  • price: number = 100
  • quantity: string = '100'
  • a: number = 10
  • b: number = 20
Create and export these variables:
  1. looseEqual β€” compare price and quantity with ==
  2. strictEqual β€” compare price and quantity with ===
  3. notEqualLoose β€” whether a is not equal to b using !=
  4. notEqualStrict β€” whether a is not equal to b using !==
  5. isGreater β€” whether b is greater than a
  6. isLessOrEqual β€” whether a is less than or equal to b
For the price/quantity comparisons, add a // @ts-expect-error comment on the line above each comparison so TypeScript allows comparing different types (that's part of the lesson).

Required exports

looseEqual, strictEqual, notEqualLoose, notEqualStrict, isGreater, isLessOrEqual

Completion criteria

Each export is a boolean produced by the comparison described above. Log or inspect the values with these fixtures so you can see how loose vs strict equality (and the other operators) evaluateβ€”don't hardcode the answers.

Please set the playground first

Loading "Comparisons"
Loading "Comparisons"