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.

String methodES3 (1999)Live demo
Common call
a.concat(b)
Returns
a new string
Replaces
nothing — + and template literals replace IT
Watch out
Array.prototype.concat spreads arrays; this one does not
string.concat(...strings...strings — Any number of values, each converted to a string and appended in order. With no arguments the original string is returned.type: any · default: none)
→ string

Demo

Live evaluation
Try:
Inputs
astringfirst string
bstringsecond string
Output
'Hello, '.concat('world')
'Hello, world'

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

NameTypeRequiredDescription
...stringsanyno (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

Use a template literal
Clearest when mixing text and values.
const msg = `Hello, ${name}!`;
Use +
Fine for two or three fragments.
const path = base + '/' + file;
Use join for a list
Better than reducing with concat.
const csv = fields.join(',');

Examples

1. Two strings
'a'.concat('b')
Returns
'ab'
2. Several at once
'a'.concat('b', 'c')
Returns
'abc'
3. Values coerced
'a'.concat(1, null)
Returns
'a1null'
4. No arguments
'a'.concat()
Returns
'a'
5. The + operator
'a' + 'b'
Returns
'ab'
6. Array concat differs
['a'].concat(['b'])
Returns
['a', 'b']

Pitfalls

1. It is not Array.prototype.concat
Same name, different behaviour. The array version spreads array arguments one level deep; the string version converts everything to text and appends. Passing an array to the string method gives you its comma-joined form.
Stringified
'a'.concat([1, 2])
'a1,2'
Array version
['a'].concat([1, 2])
['a', 1, 2]
2. It throws on null or undefined receivers
Calling it on a value that might be null fails, where a template literal would happily print "null". This is a general property of string methods, and concat is often reached for in exactly the string-building code where a value might be missing.
Throws
let s = null;
s.concat("x")
TypeError: Cannot read properties of null (reading 'concat')
Template literal
let s = null;
`${s}x`
'nullx'
3. Building a long string in a loop
Repeated concatenation — with concat or with += — creates a new string each time. Modern engines optimise this well, but collecting the pieces in an array and joining once is both clearer and reliably linear.
Grows quadratically in theory
for (const x of xs) out = out.concat(x);
works, but noisy
Collect and join
const out = xs.join('');
one allocation

When to use

Use it
  • Rarely — recognising it in existing code
  • A functional style where a method reference is needed
Reach for something else
  • 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

Complexity
O(n) in the total length of the result
Return
A new string; the original is untouched
CPython impl
V8: Builtins-string-concat
Memory
Engines often build a rope rather than copying immediately
Thread-safe
Single-threaded; the strings are only read

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.

History

ES3
concat added for symmetry with the array method.
ES2015
Template literals arrived and became the idiomatic way to build strings.