liveBook · Manning

Comparison

As explained in JavaScript in Depth

Language semantics and operators for testing relational ordering or equality between values.

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

How JavaScript in Depth covers it

How deep it goes
Works through loose-equality conversions, contrasts relational coercion, and explains UTF-16 string comparison and normalization.
Examples
Uses `true == '1'`, mixed-type ordering, default array sorting, and visually identical usernames with different Unicode sequences.
What it assumes
Assumes familiarity with JavaScript expressions and strings while demonstrating conversion rules through concrete comparisons.
What you can do after
Diagnose/debug with it by identifying unintended coercion, string sorting, and Unicode-equivalence failures before choosing a comparison strategy.
How it connects
Connects comparison to type coercion, strict equality, truthiness, array sorting, UTF-16, and Unicode normalization.

Also explained in

Compare how 9 books explain comparison →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.