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.
// β
Using `+` addition operator
5 + "0" ποΈ "50"
// β
Using `${}` template literal
`${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.
const str: string = "$";
const num: number = 904;
// β
Using `+` addition operator
const price: string = str + num;
console.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.
const str: string = "$";
const num: number = 904;
// β
Using `${}` template literal
const price: string = `${str}${num}`;
console.log(price); // ποΈ '$904'
I hope it helps you, All the best π.