
- Javascript 基礎教程
- Javascript - 首頁
- JavaScript - 路線圖
- JavaScript - 概述
- JavaScript - 特性
- JavaScript - 啟用
- JavaScript - 位置
- JavaScript - 語法
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - 註釋
- JavaScript - 變數
- JavaScript - let 語句
- JavaScript - 常量
- JavaScript - 資料型別
- JavaScript - 型別轉換
- JavaScript - 嚴格模式
- JavaScript - 保留關鍵字
- JavaScript 運算子
- JavaScript - 運算子
- JavaScript - 算術運算子
- JavaScript - 比較運算子
- JavaScript - 邏輯運算子
- JavaScript - 位運算子
- JavaScript - 賦值運算子
- JavaScript - 條件運算子
- JavaScript - typeof 運算子
- JavaScript - 空值合併運算子
- JavaScript - delete 運算子
- JavaScript - 逗號運算子
- JavaScript - 分組運算子
- JavaScript - yield 運算子
- JavaScript - 展開運算子
- JavaScript - 指數運算子
- JavaScript - 運算子優先順序
- JavaScript 控制流
- JavaScript - if...else
- JavaScript - while 迴圈
- JavaScript - for 迴圈
- JavaScript - for...in
- Javascript - for...of
- JavaScript - 迴圈控制
- JavaScript - break 語句
- JavaScript - continue 語句
- JavaScript - switch case
- JavaScript - 使用者自定義迭代器
- JavaScript 函式
- JavaScript - 函式
- JavaScript - 函式表示式
- JavaScript - 函式引數
- JavaScript - 預設引數
- JavaScript - Function() 建構函式
- JavaScript - 函式提升
- JavaScript - 自執行函式
- JavaScript - 箭頭函式
- JavaScript - 函式呼叫
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - 閉包
- JavaScript - 變數作用域
- JavaScript - 全域性變數
- JavaScript - 智慧函式引數
- JavaScript 物件
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - String
- JavaScript - Array
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Handler
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Set
- JavaScript - WeakSet
- JavaScript - Map
- JavaScript - WeakMap
- JavaScript - 可迭代物件
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - 模板字面量
- JavaScript - 帶標籤的模板
- 面向物件的 JavaScript
- JavaScript - 物件
- JavaScript - 類
- JavaScript - 物件屬性
- JavaScript - 物件方法
- JavaScript - 靜態方法
- JavaScript - 顯示物件
- JavaScript - 物件訪問器
- JavaScript - 物件建構函式
- JavaScript - 原生原型
- JavaScript - ES5 物件方法
- JavaScript - 封裝
- JavaScript - 繼承
- JavaScript - 抽象
- JavaScript - 多型
- JavaScript - 解構賦值
- JavaScript - 物件解構
- JavaScript - 陣列解構
- JavaScript - 巢狀解構
- JavaScript - 可選鏈
- JavaScript - 全域性物件
- JavaScript - Mixins
- JavaScript - 代理
- JavaScript 版本
- JavaScript - 歷史
- JavaScript - 版本
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript 非同步
- JavaScript - 非同步
- JavaScript - 回撥函式
- JavaScript - Promise
- JavaScript - Async/Await
- JavaScript - 微任務
- JavaScript - Promise 化
- JavaScript - Promise 鏈式呼叫
- JavaScript - 定時事件
- JavaScript - setTimeout()
- JavaScript - setInterval()
- JavaScript Cookie
- JavaScript - Cookie
- JavaScript - Cookie 屬性
- JavaScript - 刪除 Cookie
- JavaScript 瀏覽器 BOM
- JavaScript - 瀏覽器物件模型
- JavaScript - Window 物件
- JavaScript - Document 物件
- JavaScript - Screen 物件
- JavaScript - History 物件
- JavaScript - Navigator 物件
- JavaScript - Location 物件
- JavaScript - Console 物件
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript 事件
- JavaScript - 事件
- JavaScript - DOM 事件
- JavaScript - addEventListener()
- JavaScript - 滑鼠事件
- JavaScript - 鍵盤事件
- JavaScript - 表單事件
- JavaScript - Window/Document 事件
- JavaScript - 事件委託
- JavaScript - 事件冒泡
- JavaScript - 事件捕獲
- JavaScript - 自定義事件
- JavaScript 錯誤處理
- JavaScript - 錯誤處理
- JavaScript - try...catch
- JavaScript - 除錯
- JavaScript - 自定義錯誤
- JavaScript - 擴充套件錯誤
- JavaScript 重要關鍵字
- JavaScript - this 關鍵字
- JavaScript - void 關鍵字
- JavaScript - new 關鍵字
- JavaScript - var 關鍵字
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM 方法和屬性
- JavaScript - DOM 文件
- JavaScript - DOM 元素
- JavaScript - DOM 屬性 (Attr)
- JavaScript - DOM 表單
- JavaScript - 更改 HTML
- JavaScript - 更改 CSS
- JavaScript - DOM 動畫
- JavaScript - DOM 導航
- JavaScript - DOM 集合
- JavaScript - DOM NodeList
- JavaScript - DOM DOMTokenList
- JavaScript 其他
- JavaScript - Ajax
- JavaScript - 非同步迭代
- JavaScript - Atomics 物件
- JavaScript - rest 引數
- JavaScript - 頁面重定向
- JavaScript - 對話方塊
- JavaScript - 頁面列印
- JavaScript - 驗證
- JavaScript - 動畫
- JavaScript - 多媒體
- JavaScript - 圖片地圖
- JavaScript - 瀏覽器
- JavaScript - JSON
- JavaScript - 多行字串
- JavaScript - 日期格式
- JavaScript - 獲取日期方法
- JavaScript - 設定日期方法
- JavaScript - 模組
- JavaScript - 動態匯入
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - 淺複製
- JavaScript - 呼叫棧
- JavaScript - 引用型別
- JavaScript - IndexedDB
- JavaScript - 點選劫持攻擊
- JavaScript - 柯里化
- JavaScript - 圖形
- JavaScript - Canvas
- JavaScript - 防抖
- JavaScript - 效能
- JavaScript - 風格指南
JavaScript - 多行字串
多行字串是跨越多行的 JavaScript 字串。在程式中使用多行字串使其更易於閱讀和維護。在 JavaScript 中,建立多行字串最簡單的方法是使用模板字面量(模板字串)。模板字面量在 ECMAScript 2015 (ES6) 中引入。在引入模板字面量之前,多行字串是透過使用 + 運算子連線多個字串來建立的。
在 JavaScript 中,字串是由包含字母、數字和特殊字元的字元序列組成的。我們可以使用單引號 (')、雙引號 (") 或反引號 (`) 字元建立字串。
使用模板字面量建立多行字串
模板字面量是建立 JavaScript 多行字串的最佳方法。模板字面量用反引號 (`)括起來。模板字面量包含字串和佔位符。模板字面量有時也稱為模板字串。
模板字面量的一個簡單示例如下所示:
`This is a template literal enclosed by backtick characters.`
現在讓我們使用模板字面量建立一個多行字串:
let multilineString = `This is a multiline string created using template literal.`
在上面的 JavaScript 程式碼片段中,我們建立了一個包含三行的多行字串。我們將此多行字串賦值給名為multilineString
的變數。
示例
在下面的示例中,我們使用模板字面量建立了一個多行字串,並在網頁控制檯中顯示該字串。
let mulString = `This is a multiline string created using template literal.`; console.log(mulString);
輸出
This is a multiline string created using template literal.
示例
在下面的示例中,我們嘗試在網頁上顯示使用模板字面量建立的多行字串。我們使用<br>進行換行。
<!DOCTYPE html> <html> <body> <p id = "output"></p> <script> let mulString = `This is a multine <br> string created using template literal <br> and displayed on the webpage.`; document.getElementById("output").innerHTML = mulString; </script> </body> </html>
輸出
This is a multine string created using template literal and displayed on the webpage.
使用 + 運算子建立多行字串
我們還可以透過使用+ 運算子連線各個字串來在 JavaScript 中建立多行字串。要建立換行符,我們可以使用跳脫字元 \n 或<br>。
您可以連線用單引號或雙引號定義的字串。
讓我們看看下面的例子:
示例
在這個例子中,我們透過連線三個單獨的字串建立了一個多行字串。我們在各個字串的末尾使用了跳脫字元 (\n) 來換行。
let mulString = "This is a multiline string\n" + "created by concatenating the individual strings\n" + "and using \\n to break the line."; console.log(mulString);
輸出
This is a multiline string created by concatenating the individual strings and using \n to break the line.
示例
在下面的示例中,我們透過連線三個字串建立了一個多行字串。我們使用<br>進行換行。
<!DOCTYPE html> <html> <body> <p id = "output"></p> <script> let mulString = "This is a multiline string <br>" + "created by concatenating the individual strings<br>" + "and line break."; document.getElementById("output").innerHTML = mulString; </script> </body> </html>
輸出
This is a multiline string created by concatenating the individual strings and line break.
使用 \ 運算子建立多行字串
我們可以在 JavaScript 中使用反斜槓 (\) 運算子建立多行字串。我們可以使用跳脫字元 (\n) 來換行。
示例
試試下面的 JavaScript 示例:
let mulString = "This is a multiline string\n\ created using the backslash operator\n\ and escape character to break the line."; console.log(mulString);
輸出
This is a multiline string created using the backslash operator and escape character to break the line.
示例
在下面的示例中,我們使用反斜槓 (\) 運算子建立了一個多行字串。為了換行,我們使用了<br>。
<!DOCTYPE html> <html> <body> <p id = "output"></p> <script> let mulString = "This is first line of the multiline string <br>\ This is second line of the multiline string <br> \ This is the last line of multiline string."; document.getElementById("output").innerHTML = mulString; </script> </body> </html>
輸出
This is first line of the multiline string This is second line of the multiline string This is the last line of multiline string.