concat()
方法将给定的参数连接到给定的字符串。
示例
let emptyString = "";
// joint arguments string
let joinedString = emptyString.concat("JavaScript", " is", " fun.");
console.log(joinedString);
// Output: JavaScript is fun.
concat() 语法
concat()
方法的语法是
str.concat(str1, ..., strN)
这里,str
是一个字符串。
concat() 参数
concat()
方法接受任意数量的字符串作为参数,将它们连接到 str
。
concat() 返回值
- 返回一个新字符串,其中包含提供的字符串的组合文本。
注意: 强烈建议使用赋值运算符(如 +
和 +=
)而不是 concat()
方法。
示例:使用 concat() 方法
console.log("".concat({})); // [object Object]
console.log("".concat(null)); // null
console.log("".concat(true)); // true
console.log("".concat(4, 5)); // 45
let str1 = "Hello";
let str2 = "World";
// concatenating two strings
let newStr = str1.concat(", ", str2, "!");
console.log(newStr); // Hello, World!
输出
[object Object] null true 45 Hello, World!
另请阅读