A value can be truthy without comparing equal to `true`. For example, `'yes'` enters a truthy branch, but `'yes' == true` is false. Comparison follows operator-specific rules, not a single notion of whether two values “match.” section 4.0
Loose equality (`==`) can convert operands before comparing them. In `true == '1'`, the boolean becomes `1`, then the string becomes `1`; strict equality (`===`) performs no such coercion. Use `===` when those conversions are not intended. section 4.5 section 4.6
|
Pass
|
Comparison
|
Matching Clause
|
Conversion
|
|
1
|
true == '1'
|
Left operand is a boolean
|
ToNumber(true) yields 1
|
|
2
|
1 == '1'
|
Number on the left, string
on the right
|
ToNumber('1') yields 1
|
|
3
|
1 == 1
|
Both operands the same
|
None, hands off to ===
|
Trace each pass of `true == '1'` to see why an apparently simple equality check requires two conversions. section 4.5
Ordering has different rules. Two strings compare in string order, so `'10' < '9'` is true. Compare `'10'` with the number `9`, and the string is converted to a number, making the result false. The default sort of `['10', '2', '1']` likewise produces `['1', '10', '2']`, not numeric order. section 4.5.2
Equal-looking usernames
A username containing `é` can encode that character as U+00E9 or as U+0065 followed by U+0301. The two strings look alike but have different UTF-16 code-unit sequences, so a direct equality check can miss an existing username. Normalize both stored and incoming names before comparing them; `normalize()` uses NFC by default. section 2.1.2
Before relying on a comparison:
- Check whether the operator coerces its operands; equality and ordering do not share one conversion rule. section 4.5 section 4.5.2
- Normalize text when equivalent Unicode sequences must compare alike; strict equality alone does not normalize strings. section 2.1.2 section 4.6
- Convert data once at system boundaries rather than repeatedly in loops or sort functions. section 4.6








