liveBook · Manning

Data conversion

As explained in Pro Angular 16

Type coercion is the implicit conversion of a value from one data type to another during an operation or comparison.

Data conversion changes a value from one type to another. JavaScript performs some conversions implicitly, which can produce surprising results, so explicit conversion makes the intended operation clear. section 3.4.7

Preventing accidental concatenation

The expression 5 + 5 produces the number 10, but 5 + "5" produces the string "55". Convert deliberately with (5).toString() or String(5) when concatenating, and with Number("5") or parseInt("5") when calculating.

How Pro Angular 16 covers it

How deep it goes
Explains implicit conversion, demonstrates number-string cases, and lists conversion methods without descending into runtime conversion rules.
Examples
Demonstrates 5 + 5 versus 5 + "5", explicit number-string conversion, and fallback handling for an undefined value and zero.
What it assumes
Assumes basic JavaScript expressions, operators, variables, and TypeScript type inference.
What you can do after
Apply explicit conversion with toString, String, Number, parseInt, and parseFloat to control operation results.
How it connects
Connects data conversion with operator precedence, equality, truthy and falsy values, nullish coalescing, and optional chaining.

Also explained in

Compare how 21 books explain data conversion →

Related concepts

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