String.prototype.concat
Joins strings together.
Available since: ES3
Syntax
str.concat(str1, str2, /* … */)Parameters
| Parameter | Type | Description |
|---|---|---|
| str1, … | string | Strings to append. |
Return value
stringA new string.
Examples
Joining three strings:
console.log('Hello'.concat(', ', 'Anna'));Output:
Hello, AnnaThe same with the + operator and a template literal:
const name = 'Anna';
console.log('Hello, ' + name);
console.log(`Hello, ${name}`);Output:
Hello, Anna
Hello, AnnaNotes
In practice concat() is rarely used: the + operator and template literals are shorter and clearer.
See also
- Template literal — A string in backticks with substitutions and line breaks.
- Array.prototype.join — Joins array elements into a string.
- Array.prototype.concat — Merges arrays into a new array.
In the tutorial
- Strings — Strings in JavaScript — quotes, joining, template literals, length, accessing characters and the first methods.