Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

JavaScript - String concat() method example

0 contributions
6 points

Concat method is used to join two or more strings in JavaScript. The method appends passed arguments to right side of string. 

// ONLINE-RUNNER:browser;

var a = 'abc';
var b = 'def';

console.log( a.concat()  ); // abc
console.log( a.concat(b) ); // abcdef
console.log( b.concat(a) ); // defabc

1. Documentation

SyntaxString.prototype.concat([string2[, string3[, ..., stringN]]])
Parametersstring2...stringN - optional string type agruments (primitive value).

Combining the string on which the function was called and arguments of the string type (primitive values). Further arguments to the function are appended on the right.

If no arguments passed it returns same string.

Descriptionconcat takes zero or more arguments and returns containing the text of the combined strings.

2. Alternative concatenation approach example

Note: it is strongly recommended to use += operator instead concat() method to get better peformance of operations.

// ONLINE-RUNNER:browser;

var text = 'abc1';

text += 'abc2';
text += 'abc3';
text += 'abc4';


3. Concatenation performance example

In this section presented program execution time depends of JavaScript implementation.

Note: some browsers can be optimized with concat() method.

// ONLINE-RUNNER:browser;

var text1 = 'abc1';
var text2 = 'abc1';

var texts = [
    'abc2', 'abc3', 'abc4'

// -----------------------------------------------
// += operator test

var t1 = new Date();

for (var i = 0; i < 100000; ++i) {
    text1 += texts[i % texts.length];

var t2 = new Date();
var dt = t2 - t1;

console.log(dt + 'ms  <- += operator test time');

// -----------------------------------------------
// concat method test

var t1 = new Date();

for (var i = 0; i < 100000; ++i) {
    text2 = text2.concat(texts[i % texts.length]);

var t2 = new Date();
var dt = t2 - t1;

console.log(dt + 'ms  <- concat method test time');


0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more