- Mastering Angular Components
- Gion Kunz
- 212字
- 2021-07-23 17:23:40
Template strings
Template strings are very simple, but they are an extremely useful addition to the JavaScript syntax. They serve three main purposes:
- Writing multiline strings
- String interpolation
- Tagged template strings
Before template strings, it was quite verbose to write multiline strings. You needed to concatenate pieces of strings and append a new-line character yourself to the line endings:
const header = '<header>\n' +
' <h1>' + title + '</h1>\n' +
'</header>';
Using template strings, we can simplify this example a lot. We can write multiline strings, and we can also use the string interpolation functionality for our title variable that we used to concatenate earlier:
const header = `
<header>
<h1>${title}</h1>
</header>
`;
Note the back ticks instead of the previous single quotes. Template strings are always written between back ticks, and the parser will interpret all characters in-between them as part of the resulting string. This way, the new-line characters present in our source file will also be part of the string automatically.
You can also see that we have used the dollar sign, followed by curly brackets to interpolate our strings. This allows us to write arbitrary JavaScript within strings and helps a lot while constructing HTML template strings.
You can read more about template strings on the Mozilla Developer Network.
- 物聯網與北斗應用
- Building E-commerce Sites with VirtueMart Cookbook
- 智能網聯汽車V2X與智能網聯設施I2X
- 通信簡史:從信鴿到6G+
- Building RESTful Web services with Go
- 智慧光網絡:關鍵技術、應用實踐和未來演進
- Spring 5.0 Projects
- Unity Artificial Intelligence Programming
- 端到端QoS網絡設計
- 工業互聯網創新實踐
- 互聯網安全的40個智慧洞見(2016)
- Hands-On Docker for Microservices with Python
- Web用戶查詢日志挖掘與應用
- 計算機通信網絡安全
- Python API Development Fundamentals