乐闻世界logo
搜索文章和话题

Is string interpolation possible in TypeScript?

浏览0
2024年8月7日 13:58

Yes, string interpolation is possible in TypeScript, and it is also known as template literals or template strings. It allows you to embed expressions within string literals, using backticks (`) instead of single or double quotes. To include a variable or expression within the string, use the ${expression} syntax. This feature makes it easier to create more readable and concise strings without resorting to string concatenation.

Here's an example of string interpolation in TypeScript:

Image 15-06-23 at 1.05 AM.webp

In this example, the message variable uses a template literal to construct a complete string with interpolated variables firstName, lastName, and age. The output will be:
Hello, my name is John Doe and I'm 30 years old.

标签:TypeScript