String.prototype.concat()

String 值的 concat() 方法将字符串参数连接到该字符串并返回一个新字符串。

¥The concat() method of String values concatenates the string arguments to this string and returns a new string.

Try it

语法

¥Syntax

js
concat()
concat(str1)
concat(str1, str2)
concat(str1, str2, /* …, */ strN)

参数

¥Parameters

str1, …, strN

一个或多个字符串连接到 str

返回值

¥Return value

包含所提供字符串的组合文本的新字符串。

¥A new string containing the combined text of the strings provided.

描述

¥Description

concat() 函数将字符串参数连接到调用字符串并返回一个新字符串。

¥The concat() function concatenates the string arguments to the calling string and returns a new string.

如果参数不是字符串类型,则在连接之前将它们转换为字符串值。

¥If the arguments are not of the type string, they are converted to string values before concatenating.

concat() 方法与 加法/字符串连接运算符 (+, +=) 非常相似,不同之处在于 concat() 将其参数直接强制为字符串,而加法首先将其操作数强制为原语。有关详细信息,请参阅 + 运算符 的参考页。

¥The concat() method is very similar to the addition/string concatenation operators (+, +=), except that concat() coerces its arguments directly to strings, while addition coerces its operands to primitives first. For more information, see the reference page for the + operator.

示例

¥Examples

使用 concat()

¥Using concat()

以下示例将字符串组合成一个新字符串。

¥The following example combines strings into a new string.

js
const hello = "Hello, ";
console.log(hello.concat("Kevin", ". Have a nice day."));
// Hello, Kevin. Have a nice day.

const greetList = ["Hello", " ", "Venkat", "!"];
"".concat(...greetList); // "Hello Venkat!"

"".concat({}); // "[object Object]"
"".concat([]); // ""
"".concat(null); // "null"
"".concat(true); // "true"
"".concat(4, 5); // "45"

规范

Specification
ECMAScript Language Specification
# sec-string.prototype.concat

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看