Last Updated : Jul 1, 2023
The method String.concat() takes the list of strings for parameters and produces a new string that combines all the strings after concatenation.
Concatenation takes place after the parameters, if they are not strings, are first converted via the concat() method.
Some strings that are required to be combined together are the argument to this function.
As many parameters are required for this function to merge all the strings are required.
You can combine strings that are stored in multiple variables by using the concat() function.
Moreover, the concat() function can be used to join strings together with spaces.
str.concat(string2, string3, string4,......, stringN)
let str1 = 'Talkerscode' let str2 = 'Talkerstech' let str3 = 'Talkersmoney' let result1 = str1.concat(str2,str3) console.log('Result without spaces: ' +result1) let result2 = str1.concat(' ',str2,' ',str3) console.log('Result with spaces: ' +result2)
- To begin, we create three strings called str1, str2, and str3 for Talkerscode, Talkerstech, and Talkersmoney, respectively.
- After that, we combine all the strings empty of spaces using the concat function.
- Then, we combine all of the strings with spaces once more using the concat method.
- Next, we print the second result in result2 form using console.log and add spaces.
If the parameters are not strings, they must first be transformed using the concat() method before concatenation can take place.