Open In App

How to use Template Literals in TypeScript ?

Last Updated : 23 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The template literal is a concise and flexible way of creating the dynamic string in TypeScript. The meaning of dynamic strings here is that they can be created using expressions like conditional statements, variables, or any other mathematical operation results that can be dynamically calculated at runtime and embedded in the strings. The template literals can be used in the same way as we use them in Vanilla JavaScript. They are defined using the backticks(“) syntax available just below the ESC button on your keyboard.

Syntax:

`string value ${variable/Expression}`

Example: The below code will explain the use of template literals in TypeScript.

Javascript




const cmpny: string =
    "GeeksforGeeks";
const desc: string =
    "A Computer Science Portal";
const est: number = 2009;
const tempLit =
    `${cmpny}, ${desc} was established in: ${est}`;
     
console.log(tempLit);


Output:

GeeksforGeeks, A Computer Science Portal was established in: 2009

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads