liveBook · Manning

Abstract equality comparison

As explained in Essential TypeScript 5, Third Edition

Abstract equality comparison is JavaScript's equality algorithm that may convert operand types before comparing their values.

Abstract equality comparison is JavaScript’s double-equals (==) comparison. When operands have different types, JavaScript may coerce one value before comparing them. section 3.3.2

Comparing a number with a string

With hatPrice set to 100 and bootsPrice set to "100", hatPrice == bootsPrice evaluates to true because the string is converted to a number. Use === when the comparison should avoid this coercion. section 3.3.2

Keep comparison coercion separate from arithmetic coercion:

  • The + operator converts a number to a string when either operand is a string, producing concatenation such as 100100. section 3.3.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines number-string coercion and contrasts it with addition, while deferring the full specification algorithm.
Examples
Uses mixed price values, == and === comparisons, explicit Number conversion, and resulting console output.
What it assumes
Assumes familiarity with JavaScript values, operators, strings, numbers, and conditional expressions.
What you can do after
Evaluate mixed-type comparisons and recognize when explicit conversion or strict comparison prevents unintended coercion.
How it connects
Connects type coercion, type conversion, strict equality comparison, and numeric conversion.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.