In this Blog, we will learn about Template Strings, String Interpolation, and Multi-Line Strings in Typescript.
What are Template Strings in TypeScript?
Template Strings are literals delimited with backticks ` instead of single quote or double quotes.
The syntax for template literals is as follows.
Syntax :
`string text ${expression} string text`
1.They need to be enclosed in template literal syntax or a backtick.
2.The expression needs to be enclosed in curly braces and a dollar symbol ($expression).
3.The “target”: “es6” or “target”: “es2015” must be enabled in the tsconfig.json file.
4.Strings are produced by template literals always.
Example:
`Hello & Welcome to Crmonce Blogs`
String Interpolation in TypeScript :
Interpolation is the process of evaluating a string literal that contains one or more expressions. It evaluates the expression and converts the result to a string. The result is then replaced in the original string.
Example :
let EmployeeName :string = “Abcd”;
console.log(`${EmployeeName} is the greatest cricketer of all time`)
The expression used in the example above is $(Employee Name). It is first evaluated, and the answer, “Abcd,” is then substituted in its place in the final string.
Example 2 :
Multiline strings in Type Script :
We will define multiline strings in typescript using template literals. A template literal is simply a string defined with backticks (“) rather than double and single quotes.
The example below shows how to make a multiline string. Simply press enter to move on to the next line. The n character is automatically added to the string.
Example :
The multiline strings without back-quote are created by inserting a \n newline character (LF).
Example :
For any Help or Queries Contact us on info@crmonce.com or +918096556344