- 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.
- CorelDRAW X6 中文版圖形設(shè)計實戰(zhàn)從入門到精通
- 物聯(lián)網(wǎng)安全:理論、實踐與創(chuàng)新
- HCNA網(wǎng)絡(luò)技術(shù)
- JBoss EAP6 High Availability
- 智慧城市中的移動互聯(lián)網(wǎng)技術(shù)
- NB-IoT物聯(lián)網(wǎng)技術(shù)解析與案例詳解
- 物聯(lián)網(wǎng)與無線傳感器網(wǎng)絡(luò)
- Building RESTful Web services with Go
- 物聯(lián)網(wǎng)工程概論
- Learning Node.js Development
- 5G時代的大數(shù)據(jù)技術(shù)架構(gòu)和關(guān)鍵技術(shù)詳解
- 中國互聯(lián)網(wǎng)發(fā)展報告2021
- 計算機網(wǎng)絡(luò)技術(shù)
- 物聯(lián)網(wǎng)
- NB-IoT原理和優(yōu)化