在 JavaScript 的 ES6 版本中,引入了文字。 JavaScript 包含物件文字、陣列文字、數字文字、RegExp 文字等。此外,它還包含字串文字。
字串文字允許我們創建不帶任何反斜線字元的多行字串,在引號中添加任何單字或句子,以及在字串之間添加變數和數學表達式。
文法
使用者可以依照以下語法在 ECMAScript 6 中使用模板字串文字。
let string = `This is template literal string!`;
在上面的語法中,我們使用反引號 (``) 來建立模板文字字串。
範例 1(多行字串)
在下面的範例中,我們使用模板文字字串來建立多行字串。每當我們建立帶有引號的字串時,我們都需要使用「
」字元來建立新行,但是使用字串文字,我們可以透過在新行中寫入字串來實現。
在 string1 中,新行是透過在新行中寫入字串來建立的,而在 string2 中,我們使用「
」字元來建立新行。使用者可以觀察輸出中的 string1 和 string2,它們看起來是相同的。
let string1 = `This is first line. This is the second line. This is the third line. This is the fourth line.`; console.log(string1); // added character to create a multiline string. let string2 = "Welcome on the TutorialsPoint!"; console.log(string2);
範例 2(字串中的引號)
我們可以使用模板字串文字在字串內加上引號。當我們建立帶有雙引號的字串時,我們只能為該字串添加單引號,而當我們建立帶有單引號的字串時,我們也只能為該字串添加雙引號。
我們使用字串文字在 stringQuote 變數的字串中加入了單引號。
<html> <body> <h2 id="Using-the-i-template-string-literals-i-to-add-quotes-in-the-string">Using the <i>template string literals</i> to add quotes in the string.</h2> <div id = "output"></div> </body> <script> var output = document.getElementById('output'); let stringQuotes = `This is a 'template string literals' with a quote.`; output.innerHTML += stringQuotes + "<br/>"; let string1 = "This is 'similar to template string literals'." + "<br/>"; output.innerHTML += string1; </script> </html>
範例 3(字串中的變數)
在下面的範例中,我們在字串中完成了變數替換。一般來說,要在字串中使用變量,我們需要使用“ ”運算符並連接多個字串,但模板字串文字允許我們直接在字串中添加變數。我們可以在 ${} 表達式中加入變數。
在variableStr變數的值中,我們插入了name、job和timePeriod變數。
<html> <body> <h2 id="Using-the-i-template-string-literals-i-to-add-variables-in-the-string">Using the <i>template string literals </i> to add variables in the string.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let name = "Shubham"; let job = "Content writer"; let timePeriod = "1 Year"; let variableStr = `Using template string literals :- ${name} is a ${job} at TutorialsPoint from last ${timePeriod}.`; output.innerHTML += variableStr + "<br/>"; let string = "Using Quotes :- " + name + " is a " + job + " at TutorialsPoint from last " + timePeriod + ". "; output.innerHTML += string + "<br/>"; </script> </html>
範例 4(字串中的表達式)
在此範例中,我們將使用範本字串文字在字串中加入數學表達式。在 sumString 中,我們在 ${} 內加入了數學表達式。使用者可以看到我們如何在字串中對 num1 和 num2 求和。
此外,我們也對 string2 中的 2 個值進行了乘法運算。
<html> <body> <h2 id="Using-the-i-template-string-literals-i-to-add-expression-in-the-string">Using the <i> template string literals </i> to add expression in the string.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let num1 = 10; let num2 = 40; let sumString = `The sum of ${num1} and ${num2} is ${num1 + num2}`; output.innerHTML += sumString + "<br>"; let string2 = `The multiplication of 20 and 5 is ${20 * 5}`; output.innerHTML += string2 + "<br>"; </script> </html>
範例 5(字串中的 HTML)
我們可以使用範本字串文字建立一行 HTML 並將其加入到網頁中。在此範例中,我們使用字串文字建立了 HTML 列表,並使用 的innerHTML 屬性在網頁中新增行 HTML。
<html> <body> <h2 id="Using-the-i-template-string-literals-i-to-add-HTML-to-the-document">Using the <i>template string literals</i> to add HTML to the document.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let HTMLString = `<ul> <li> One </li> <li> Two </li> <li> Three </li> <li> Four </li> <li> Five </li> </ul>`; output.innerHTML = HTMLString; </script> </html>
使用者學會了在 JavaScript 中使用模板字串文字。我們已經了解如何建立多行字串、變數和表達式替換、添加引號以及使用模板字串文字建立行 HTML。
以上是如何在 ECMAScript 6 中使用模板字串文字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python更适合数据科学和机器学习,JavaScript更适合前端和全栈开发。1.Python以简洁语法和丰富库生态著称,适用于数据分析和Web开发。2.JavaScript是前端开发核心,Node.js支持服务器端编程,适用于全栈开发。

JavaScript不需要安裝,因為它已內置於現代瀏覽器中。你只需文本編輯器和瀏覽器即可開始使用。 1)在瀏覽器環境中,通過標籤嵌入HTML文件中運行。 2)在Node.js環境中,下載並安裝Node.js後,通過命令行運行JavaScript文件。

如何在Quartz中提前發送任務通知在使用Quartz定時器進行任務調度時,任務的執行時間是由cron表達式設定的。現�...

在JavaScript中如何獲取原型鏈上函數的參數在JavaScript編程中,理解和操作原型鏈上的函數參數是常見且重要的任�...

在微信小程序web-view中使用Vue.js動態style位移失效的原因分析在使用Vue.js...

在Tampermonkey中如何對多個鏈接進行並發GET請求並依次判斷返回結果?在Tampermonkey腳本中,我們經常需要對多個鏈...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載
最受歡迎的的開源編輯器

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。