模板字串
由於 ES6 中有了模板字串,JavaScript 可以使用模板字串來替代原本我們使用的引號字元。
Ex: 正常的字串
var firstName = 'Jake';
var lastName = 'Rawr';
console.log('My name is ' + firstName + ' ' + lastName);
// My name is Jake Rawr
模板字串
var firstName = 'Jake';
var lastName = 'Rawr';
console.log(`My name is ${firstName} ${lastName}`);
// My name is Jake Rawr
在模板字串中,你不需要透過 \n
來產生多行的字串,只要簡單透過 ${}
來替代就可以了,還可以計算簡單的邏輯,例如:${2 + 3}
。
你也可以使用函式來修改你的輸出的內容;例如使用標籤模板字串,它們被稱為標籤模板字串。
你或許想要閱讀和了解更多關於模板字串。
MEET THE NEW JSTIPS BOOK
You no longer need 10+ years of experience to get your dream job.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.
GET THE BOOK NOW
MEET THE NEW JSTIPS BOOK
The book to ace the JavaScript Interview.
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.
GET THE BOOK NOW