String.prototype.concat()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
concat()
會將呼叫此方法的字串以及作為參數傳遞進此方法的字串串接在一起,並將串接結果作為一個新的字串回傳。
嘗試一下
const str1 = "Hello";
const str2 = "World";
console.log(str1.concat(" ", str2));
// Expected output: "Hello World"
console.log(str2.concat(", ", str1));
// Expected output: "World, Hello"
語法
js
concat(str1)
concat(str1, str2)
concat(str1, str2, ... , strN)
參數
strN
-
要串接到
str
的字串,可以傳入一個至多個。
回傳值
此方法會回傳一個新的字串,由呼叫此方法的字串及作為參數傳入的字串組合而成。
描述
concat()
會將那些作為參數的字串串接在呼叫此方法的字串後面,並作為一個新的字串回傳。
對於原先的字串、或是回傳的字串做修改,不會讓他們的值互相影響。
如果傳入的參數不是字串型別,那在串接前會先將該參數轉換成字串。
效能
對於字串的串接,強烈建議直接使用運算子 assignment operators 來達成,
像是 +
及 +=
,而不是使用 concat()
方法。
範例
如何使用 concat()
以下的例子示範如何將那些給定的字串組合成新的字串。
js
let hello = "Hello, ";
console.log(hello.concat("Kevin", ". Have a nice day."));
// Hello, Kevin. Have a nice day.
let greetList = ["Hello", " ", "Venkat", "!"];
"".concat(...greetList); // "Hello Venkat!"
"".concat({}); // [object Object]
"".concat([]); // ""
"".concat(null); // "null"
"".concat(true); // "true"
"".concat(4, 5); // "45"
規範
Specification |
---|
ECMAScript® 2025 Language Specification # sec-string.prototype.concat |
瀏覽器相容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
concat |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.