liveBook · Manning

Number function

As explained in Pro Angular 16

The Number function converts its argument to a JavaScript numeric value.

Number converts a string argument into a JavaScript numeric value, producing either an integer or a real number. It parses strings strictly; parseInt and parseFloat are more flexible because they can ignore trailing non-number characters. section 3.4.7

Convert before adding

let myData1 = (5).toString() + String(5); let myData2 = Number("5") + parseInt("5"); console.log(`Result 1: ${myData1}, Type: ${typeof(myData1)}`); console.log(`Result 2: ${myData2}, Type: ${typeof(myData2)}`); The first expression performs string concatenation and produces "55". Number("5") converts the string before addition, so the second expression produces the numeric value 10.

Choose the conversion function according to the input and desired result:

  • Use Number(str) when the complete string must be converted strictly into an integer or real value. section 3.4.7
  • Use parseInt(str) for an integer or parseFloat(str) for an integer or real value when trailing non-number characters may be ignored. section 3.4.7

How Pro Angular 16 covers it

How deep it goes
Defines integer and real-number conversion, contrasts strict parsing with alternatives, and demonstrates output types without covering detailed parsing edge cases.
Examples
Uses Number("5") with parseInt("5") to produce numeric addition and contrasts it with string concatenation.
What it assumes
Assumes familiarity with JavaScript strings, numbers, the plus operator, and typeof output.
What you can do after
Apply it via Number() to convert complete numeric strings before performing arithmetic.
How it connects
Links string concatenation with parseInt and parseFloat as alternative string-to-number conversion functions.

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.