Javascript String concat() 字串相加

concat() 方法用來將好幾個字串相加,然後返回一個新字串。

語法:

str.concat(string2[, string3, ..., stringN])

用法:

const hello = 'Hello, ';
console.log(hello.concat('Kevin', ' have a nice day.'));
// Hello, Kevin have a nice day.

你也可以用 + 運算子來做字串相加:

const hello = 'Hello, ';
console.log(hello + 'Kevin' + ' have a nice day.');
// Hello, Kevin have a nice day.
在現代 JavaScript 開發中,處理字串連接最推薦的方法是使用 Template Literals (樣板字面值),例如:const str = \Hello, ${name}`;`。