Cơ BảnJavaScript iconJavaScript

Template literals là gì? Có những tính năng gì?

Template literals dùng backtick thay dấu nháy, hỗ trợ: interpolation ${expression}, multi-line strings, tagged templates.

Tagged templates là function nhận template strings và expressions làm arguments.

javascript
// Interpolation + multi-line:
const msg = `Hello ${name},
Welcome!`;

// Tagged template (styled-components pattern):
function css(strings, ...values) {
  return strings.reduce((acc, str, i) => acc + str + (values[i] ?? ''), '');
}
const color = 'blue';
const style = css`color: ${color}; font-size: 16px;`;

Xem toàn bộ JavaScript cùng filter theo level & chủ đề con.

Mở danh sách JavaScript