Add a number to a string in typescript
To add the number to a string in typescript, you can use the +
addition operator or ${}
template literal to add a number in a string both can add the number to a string.
1// ✅ Using `+` addition operator25 + "0" 👉️ "50"34// ✅ Using `${}` template literal5`${5}0` 👉️ "50"
Today, I’m going to show you How do I add a number to a string in typescript, as above mentioned here, I’m going to use the +
addition operator or ${}
template literal to add the number to a string.
Let’s start today’s tutorial how do you number to string in typescript?
Add a number to string in typescript using addition operator
Here, I created two variables and stored the dollar sign in the first variable and the number in the second variable, and concatenated them using the addition operator.
1const str: string = "$";2const num: number = 904;34// ✅ Using `+` addition operator5const price: string = str + num;6console.log(price); // 👉️ '$904'
Add a number to a string in typescript using template literal
Here, I created two variables and stored the dollar sign in the first variable and number in the second variable, and concatenated them using template literal.
1const str: string = "$";2const num: number = 904;34// ✅ Using `${}` template literal5const price: string = `${str}${num}`;6console.log(price); // 👉️ '$904'
I hope it helps you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.