How to convert a string to number in TypeScript?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
TypeScript provides several ways to convert a string to a number: Number() (full string conversion), parseInt() (integer parsing with radix), parseFloat() (decimal parsing), and the unary + operator (shorthand for Number()). The best choice depends on your input format and error handling needs. Number() is strictest — it returns NaN for any non-numeric string. parseInt() and parseFloat() are more lenient — they parse as much as they can from the start of the string. Always validate the result with isNaN() or Number.isFinite() to handle invalid inputs safely.
Number() Function
Number() attempts to convert the entire string. If any part is non-numeric (except leading/trailing whitespace), it returns NaN. This makes it the safest choice for strict validation.
parseInt() Function
parseInt() parses an integer from the beginning of the string. The second argument (radix) specifies the number base. Always pass 10 for decimal parsing to avoid legacy octal behavior.
parseFloat() Function
parseFloat() is similar to parseInt() but preserves the decimal part. It does not accept a radix parameter — it always parses base-10.
Unary Plus Operator
The + prefix operator is a shorthand for Number(). It is concise but can be less readable, especially for developers unfamiliar with the idiom.
Type-Safe Conversion Function
Handling User Input
Comparison Table
| Method | "123" | "3.14" | "12abc" | "" | " 42 " |
Number() | 123 | 3.14 | NaN | 0 | 42 |
parseInt(,10) | 123 | 3 | 12 | NaN | 42 |
parseFloat() | 123 | 3.14 | 12 | NaN | 42 |
+ | 123 | 3.14 | NaN | 0 | 42 |
Template Literal and String Concatenation
Common Pitfalls
- Forgetting the radix in
parseInt():parseInt("010")returns8in older JavaScript engines (octal interpretation). Always pass the radix explicitly:parseInt("010", 10)returns10. TypeScript does not warn about missing radix by default. Number("")returns 0, not NaN: An empty string converts to0withNumber()and the+operator. If your code treats0as a valid input, empty strings silently pass validation. Check for empty strings before converting.- Using
isNaN()vsNumber.isNaN(): GlobalisNaN()coerces its argument to a number first:isNaN("hello")returnstrue.Number.isNaN()does not coerce:Number.isNaN("hello")returnsfalse. UseNumber.isNaN(Number(value))for type-safe checking. - String concatenation instead of addition:
"5" + 10produces"510"(concatenation), not15(addition). When one operand is a string,+performs concatenation. Convert the string to a number first withNumber(),+, orparseInt(). - Precision loss with large numbers:
Number("9007199254740993")returns9007199254740992due to IEEE 754 double-precision limits. For integers larger thanNumber.MAX_SAFE_INTEGER(2^53 - 1), useBigInt("9007199254740993")instead.
Summary
- Use
Number(str)for strict conversion that rejects partially numeric strings like"12abc" - Use
parseInt(str, 10)for integer parsing that tolerates trailing characters - Use
parseFloat(str)for decimal parsing from the start of a string - The unary
+stris shorthand forNumber(str)— concise but less readable - Always validate the result with
Number.isNaN()orNumber.isFinite()before using it - Always pass the radix (
10) toparseInt()to avoid octal interpretation

