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
. The starter provides:
price: number = 100quantity: string = '100'a: number = 10b: number = 20
Create and export these variables:
looseEqualβ comparepriceandquantitywith==strictEqualβ comparepriceandquantitywith===notEqualLooseβ whetherais not equal tobusing!=notEqualStrictβ whetherais not equal tobusing!==isGreaterβ whetherbis greater thanaisLessOrEqualβ whetherais less than or equal tob
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,
isLessOrEqualCompletion 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.