• JavaScript Video Tutorials

JavaScript String concat() Method



The JavaScript String concat() method concatenates two or more string arguments to the current string. This method does not change the value of the existing string, but returns a new string.

You can concatenate multiple string arguments as per your needs.

Syntax

Following is the syntax of JavaScript String concat() method −

concat(str1, str2,......, strN)

Parameters

This method accepts multiple strings as parameters. The same as described below −

  • str1, str2,....., strN − The strings to be concatenated.

Return value

This method returns a new string after concatenating.

Example 1

In the following program, we use the JavaScript String concat() method to concatenate two strings "Tutorials" and "Point".

<html>
<head>
<title>JavaScript String concat() Method</title>
</head>
<body>
<script>
   const str1 = "Tutorials";
   const str2 = "Point";
   const new_str = str1.concat(str2);
   document.write("str1 = ", str1);
   document.write("<br>str2 = ", str2);
   document.write("<br>new_str(after concatenate) = ", new_str);
</script>
</body>
</html>

Output

The above program returns a new string "TutorialsPoint" after concatenate.

str1 = Tutorials
str2 = Point
new_str(after concatenate) = TutorialsPoint

Example 2

Following is another example of the String concat() method. In this example, we concatenate multiple strings: "Welcome", "to", "Tutorials", and "Point", along with the white spaces to separate them.

<html>
<head>
<title>JavaScript String concat() Method</title>
</head>
<body>
<script>
   const str1 = "Welcome";
   const str2 = "to";
   const str3 = "Tutorials";
   const str4 = "Point";
   document.write("str1 = ", str1);
   document.write("<br>str2 = ", str2);
   document.write("<br>str3 = ", str3);
   document.write("<br>str4 = ", str4);
   const new_str = str1.concat(" ",str2," ",str3," ",str4);
   document.write("<br>Concatenated string(with white-sapce separated) = ", new_str);
   const new_str1 = str1.concat(str2,str3,str4);
   document.write("<br>Concatenated string(without white-sapce separated) = ", new_str1);
</script>
</body>
</html>

Output

After executing the above program, it will concatenate all the strings and returns a new string as −

str1 = Welcome
str2 = to
str3 = Tutorials
str4 = Point
Concatenated string(with white-sapce separated) = Welcome to Tutorials Point
Concatenated string(without white-sapce separated) = WelcometoTutorialsPoint

Example 3

Let's see another example of how to use the JavaScript String concat() method. Using this method, we can concatenate the strings "Hello" and "World" and separate them with a comma(,).

<html>
<head>
<title>JavaScript String concat() Method</title>
</head>
<body>
<script>
   document.write("Hello".concat(", ", "World"));
</script>
</body>
</html>

Output

The above program returns "Hello, World" in the output −

Hello, World
Advertisements