Warning: session_start(): open(/tmp/sess_af8c0ff26b01b40e4b05cbb3404dcfdf, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Concatenate A String And A Number In Typescript - LearnShareIT

How To Concatenate A String And A Number In Typescript

Concatenate a String and a Number in TypeScript

Concatenate a string and a number in TypeScript will be very helpful when you are working with string and number, like when you want to show a result that has both a number and string. So how to do it? Let’s go into detail.

Concatenate a string and a number in TypeScript

Remember that when you concatenate a string and number the result will always be a string type. 

Use template literals

Template literals provided in ES6 will help you pass an expression in a string. To use template literals, you have to wrap inside a double backtick ( ` ). Then with expression, you have to wrap inside a dollar sign and double curly braces “{}”. So we can apply that to pass in a variable containing the number and string you want.

Example:

const aString: string = "Hello From Learn Share IT";
const aNumber: number = 10;
const anotherString: string = " times";
const concat1: string = `${aString}${aNumber}${anotherString}`;
console.log(concat1);

Output:

[LOG]: "Hello From Learn Share IT10 times" 

Use addition operators

Typescript provided us with many valuable operators that help you can work with. The addition operator (+) can help you concatenate a string and a number. The logic is that if you use addition operators with two numbers, that will be plus two. If you are using with string and a number, then it will concatenate the string and number.

Example:

const aString: string = "Hello From Learn Share IT";
const aNumber: number = 10;
const anotherString: string = " times";
const concat1: string = aString + aNumber + anotherString;
console.log(concat1);

Output:

[LOG]: "Hello From Learn Share IT 10 times" 

Use concat() method

You can also use the concat() method to concatenate strings and numbers.

Syntax: 

str.concat(str1,strn)

Example:

const aString: string = "Hello From Learn Share IT";
const aNumber: number = 10;
const anotherString: string = " times";
const converted: string = aNumber.toString();
const concat1 = aString.concat(aNumber, anotherString);
console.log(concat1);

Output:

[LOG]: "Hello From Learn Share IT 10 times"

If you have a number-type variable, you want to concatenate that variable value with some string and then assign it again to the variable. Make sure you use union type to set both number and string type for that variable.

Example:

let var1: number | string = 1;
const var2: string = "0";
var1 = var1 + var2;
console.log(var1);

Output:

[LOG]: "10"

Summary 

In this tutorial, we explained how to concatenate a string and a number in TypeScript. You can use template literals if you want to insert a number inside a string or use addition operators. Let’s try these methods to get your desired results!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *