How to perform string interpolation in TypeScript?
How to perform string interpolation in TypeScript?
To perform string interpolation in TypeScript, you use template literals, which are string literals allowing embedded expressions. These template literals are enclosed by the backtick (`) character instead of the traditional single (' ') or double (" ") quotes. Within these literals, you can insert placeholders for variables or expressions using the syntax ${expression}.
Here's a basic example to illustrate string interpolation in TypeScript:
let name = "John";
let greeting = `Hello, ${name}!`;
console.log(greeting); // Outputs: Hello, John!
In the example above, the variable name is interpolated into the string literal using the ${name} syntax within the backticks. This results in a dynamic string that includes the value of the name variable.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.