String.prototype.concat()
Included for completeness rather than daily use. It does exactly what + does, more verbosely, and the array method of the same name behaves quite differently.
Demo
There is nothing surprising here, which is rather the point — concat appends, and that is all. Every one of these cases is written more clearly as a + b or as a template literal. The method is worth knowing mainly so that you recognise it in older code, and so that you do not confuse it with Array.prototype.concat, which flattens array arguments one level instead of appending them as text.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| ...strings | any | no (none) | Any number of values, each converted to a string and appended in order. With no arguments the original string is returned. |
Return value
string — A new string with every argument appended in order. Non-string arguments are converted to strings first.
Common patterns
const msg = `Hello, ${name}!`;
const path = base + '/' + file;
const csv = fields.join(',');
Examples
Pitfalls
'a'.concat([1, 2])
['a'].concat([1, 2])
let s = null; s.concat("x")
let s = null; `${s}x`
for (const x of xs) out = out.concat(x);
const out = xs.join('');
When to use
- Rarely — recognising it in existing code
- A functional style where a method reference is needed
- Mixing text and values → a template literal
- Two or three fragments → the + operator
- Joining a list → Array.prototype.join
- The receiver may be null → a template literal, which coerces
Notes
FAQ
No. Engines optimise + heavily and it is the form they expect, so concat is at best equal and often slightly worse. Choose on readability, which also favours + and template literals.