JavaScript String concat() Method
Last Updated :
14 Mar, 2024
The concat()
method in JavaScript is used to concatenate two or more strings and return a new string containing the concatenated values.
It does not change the original strings; instead, it returns a new string with the concatenated values.
Syntax:
str.concat(string2, string3, string4,......, stringN)
Parameters
The arguments to this function are the strings that need to be joined together. The number of arguments to this function is equal to the number of strings to be joined together.
Return value:
Returns a new string that is the combination of all the different strings passed to it as the argument.
JavaScript String concat() Method Examples
Example 1: Merging Strings with JavaScript’s concat() Method
The func()
function utilizes the concat()
method to merge strings together. Initially, it defines the original string “Geeks”, then concatenates it with ” for” and ” Geeks”, resulting in the output “Geeks for Geeks” when executed.
JavaScript
// JavaScript concat() method to
// merge strings together
function func() {
// Original string
let str = 'Geeks';
// Joining the strings together
let value = str.concat(' for', ' Geeks');
console.log(value);
}
func();
Example 2: Concatenating Multiple Strings using JavaScript’s concat() Method
The code concatenates strings str1
, str2
, and str3
together. result1
combines them without spaces, while result2
concatenates them with spaces. The results are then logged to the console.
JavaScript
let str1 = 'Geeks'
let str2 = 'For'
let str3 = 'Geeks'
// Concating all the strings together without spaces
let result1 = str1.concat(str2, str3)
console.log('Result without spaces: ' + result1)
// Concating all the strings together with spaces
let result2 = str1.concat(' ', str2, ' ', str3)
console.log('Result with spaces: ' + result2)
OutputResult without spaces: GeeksForGeeks
Result with spaces: Geeks For Geeks
We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.
Supported Browsers:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...