Convert a Number to a String in TypeScript

Convert a Number to a String in TypeScript

Glasshost
2 min readMay 5, 2023

TypeScript is a superset of JavaScript that adds static typing to the language. It is widely used by developers to write large-scale applications. In this guide, we will learn how to convert a number to a string in TypeScript.

Method 1: Using toString() Method

The toString() method is used to convert a number to a string in TypeScript. The syntax for using the toString() method is as follows:

let num: number = 123;
let str: string = num.toString();

In the above example, we have defined a variable ‘num’ of type number and initialized it with the value 123. We have used the toString() method to convert the number to a string and assigned it to a variable ‘str’ of type string.

Method 2: Using String() Function

The String() function is another way to convert a number to a string in TypeScript. The syntax for using the String() function is as follows:

let num: number = 123;
let str: string = String(num);

In the above example, we have defined a variable ‘num’ of type number and initialized it with the value 123. We have used the String() function to convert the number to a string and assigned it to a variable ‘str’ of type string.

Conclusion

In this guide, we have learned two methods to convert a number to a string in TypeScript. The toString() method and the String() function are both simple and easy to use. You can choose any method according to your preference.

--

--

Glasshost

Hey, I am Shivam Kumar, Founder of GlassHost.tech. We offer some very competitive hosting solutions and solve coding issues/problems. Visit Glasshost.tech now.