JavaScript concat() method is used to join two or more strings together without changing the original strings and returning a new string.
Syntax:
str.concat(string2, string3, string4,......, stringN)
Arguments: 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: This function returns a new string that is the combination of all the different strings passed to it as the argument.
Below is an example of the concat() Method.
Example 1: In this example. we will merge Strings using concat() Method.
JavaScript
function func() {
let str = 'Geeks';
let value = str.concat(' for', ' Geeks');
console.log(value);
}
func();
|
Output:
Geeks for Geeks
Example 2: In this example. we will merge Strings using concat() Method.
JavaScript
function func() {
let str = 'It';
let value = str.concat(' is', ' a', ' great', ' day.');
console.log(value);
}
func();
|
Output:
It is a great day.
The concat() function can also be used to concat strings stored in two or more variables. Also, spaces can also be added to the strings using the concat() function.
Example 3: In this example, we will see the concatenation of two strings stored in different variables also we will see how to include spaces between the strings.
Javascript
let str1 = 'Geeks'
let str2 = 'For'
let str3 = 'Geeks'
let result1 = str1.concat(str2, str3)
console.log('Result without spaces: ' + result1)
let result2 = str1.concat(' ', str2, ' ', str3)
console.log('Result with spaces: ' + result2)
|
Output:
Result 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:
- Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 4 and above
- Opera 4 and above
- Safari 1 and above
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
22 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...