JavaScript String concat() function is used to join two or more strings together without modifying the original string and returns a new string.
Syntax
string.concat(string1, string2, ..., stringX)
Parameters
string1, string2: The strings to be joined.
Return Value
It returns a new string that combines all the different strings passed to it as the argument.
Visual Representation
Example 1: How to Use String concat() Method
let text = 'App';
result = text.concat('Dividend');
console.log(result);
console.log(text);
Output
AppDividend
App
Example 2: Concatenating multiple strings
let text = 'Hello';
let text2 = 'App';
let text3 = 'Dividend';
result = text.concat(text2,text3);
result2 = text.concat(' ',text2, ' ', text3);
console.log('Without space: ' ,result);
console.log('With space: ', result2);
Output
Without space: HelloAppDividend
With space: Hello App Dividend
We can also do the same thing using the + operator.
let resultplus = text + text2 + text3;
console.log(resultplus);
Output
HelloAppDividend
Example 3: Concatenating with an empty string
let text = '';
console.log(text.concat('Hello', 'App', 'Dividend',));
Output
HelloAppDividend
In the above example, concat() method concatenates multiple strings ‘Hello’, ‘App’, and ‘Dividend‘ with an initially empty string text. Since the text is empty, it doesn’t contribute any characters to the result. Therefore, the output is HelloAppDividend.
Browser Compatibility
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Opera 4 and above
- Safari 1 and above