How to use Template Literals In Typescript
In this approach, template literals are used by enclosing the string within backticks (“). Variables like website and category are added directly into the string using ${} syntax. The output message variable is then printed to the console.
Syntax:
let result: string = `String content with ${variable1} and ${variable2}`;
Example: The below example uses Template Literals to format strings in TypeScript.
Javascript
let website: string = "w3wiki" ; let category: string = "Programming" ; let message: string = `Visit ${website} for ${category} articles.`; console.log(message); |
Output:
"Visit w3wiki for Programming articles."
How to Format Strings in TypeScript ?
In TypeScript, the formatting of strings consists of structuring and concatenating the strings to make them in a properly readable format. We can use various approaches to format the strings in TypeScript.
There are several ways to format the string in TypeScript which are as follows:
Table of Content
- Using Concatenation (+)
- Using Template Literals
- Using eval() function
Contact Us