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
