String.prototype.concat()

Joins one or more values onto the end of a string and returns the combined new string.

Signature
concat(str1)
concat(str1, str2)
concat(str1, str2, /* …, */ strN)
  • Added in JavaScript ECMAScript 3

Parameters

ParameterTypeDescription
$str1, …, strNoptionalstringOne or more values to concatenate onto the calling string. Non-string arguments are coerced directly to strings. Calling concat() with no arguments is permitted but does nothing observable, since strings are immutable.

Return value

stringA new string containing the text of the calling string followed by the text of each argument.

concat() returns a new string; it never mutates the receiver, because strings are immutable. In most code the + operator, +=, or a template literal is shorter and just as fast — modern engines optimise all of them into the same rope structure.

The one real difference from +

concat() coerces each argument directly to a string, while + first converts operands to primitives and only then decides between numeric addition and concatenation. For an object with a valueOf(), that changes the answer: "" + obj may use the numeric primitive, whereas "".concat(obj) calls toString(). It also means concat() can never accidentally add numbers — "".concat(4, 5) is "45".

When it earns its place

Mainly when the pieces already live in an array and you want to spread them: "".concat(...parts). Even then, parts.join("") is usually clearer and avoids the argument-count limit that spreading a very large array can hit.

For building a string in a loop, += or pushing into an array and joining at the end both read better than repeated concat() calls. Only concat()'s return value carries the result — ignoring it is a no-op.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Spread an array of fragments into a single string without join()
  • Force toString() coercion on values that also define valueOf()
  • Append several pieces in one call inside a fluent chain
  • Concatenate values of mixed type without risking numeric addition

Frequently asked questions

Is concat() faster than the + operator for strings?

No. Modern engines optimise + and += heavily, and concat() is typically the same speed or slower. Choose based on readability, not performance.

Why did my string not change after calling concat()?

Strings are immutable, so concat() returns a new string rather than modifying the original. Assign the result: s = s.concat("x").

What is the difference between String concat() and Array concat()?

Array.prototype.concat() flattens array arguments one level into a new array; String.prototype.concat() coerces every argument to a string, so an array argument becomes its comma-joined text.

More examples

concat() vs the + operator on an object

concat() converts with ToString, so it calls toString(). The + operator asks for a primitive first, gets the number from valueOf(), and concatenates that instead — or adds it outright when the other operand is a number.

Example · javascript
const money = {
  valueOf: () => 42,
  toString: () => "$42.00",
};

console.log("".concat(money)); // "$42.00" — toString()
console.log("" + money); // "42"     — valueOf() first
console.log(`${money}`); // "$42.00" — template literals prefer toString()
console.log(1 + money); // 43 — numeric addition

Building a string: the readable options

join() handles separators that concat() cannot, += builds incrementally, and the template literal mixes text with expressions. concat() only wins when spreading an existing array with no separator.

Example · javascript
const rows = ["id", "name", "email"];

console.log(rows.join(",")); // "id,name,email"
console.log("".concat(...rows)); // "idnameemail"

let out = "";
for (const r of rows) out += r + ";";
console.log(out); // "id;name;email;"

console.log(`cols=${rows.length}: ${rows.join(" | ")}`);
// "cols=3: id | name | email"

Discussion

  • Be the first to comment on this lesson.