Skip to content

Template Literal

一句话解释

template literal 是 字符串 enclosed in backticks 那个 supports multi-行 文本 and ${表达式} interpolation.

用大白话说

Template literals 是字符串创建使用 backticks (` ) 相反的 单个 or 双 quotes. 它们 offer two features 那个 regular 字符串做 不: multi-行文本 (行 breaks 是 preserved 不使用 \n) and 表达式 interpolation 使用 ${expression}`.

Interpolation evaluates any JavaScript expression inside ${} and inserts the result into the string. This is cleaner than string concatenation: `Hello, ${name}! instead of "Hello, " + name + "!"`.

Template literals can be tagged — a function can process the literal and interpolated values to create custom string formatting (e.g., for HTML escaping or internationalization).

示例

javascript
const name = "Ada";
const age = 36;
const msg = `Name: ${name}\nAge: ${age + 1}`;
console.log(msg);
// "Name: Ada\nAge: 37"

Backticks 允许 ${} interpolation and multi-行 文本.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表