Home >>JavaScript String Methods >JavaScript String concat() Method

JavaScript String concat() Method

JavaScript String concat() Method

JavaScript concat() method is used to join two or more strings together. It does not change the existing string but returns a new string containing the text of the newly joined string.

Syntax:
string.concat(string1, string2, ..., stringX);

Parameter Values

Parameter Description
string1, string2, ..., stringX This is a required parameter. It defines the strings to be joined.

Browser Support

Method Chrome Edge Firefox Safari Opera
concat() Yes Yes Yes Yes Yes
Here is an example of concat() method:
<html>
<body>
<p>Click the button to join two strings.</p>
<button onclick="myFunction()">Click</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str1 = "PHP ";
  var str2 = "Python";
  var res = str1.concat(str2);
  document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
Output:

Click the button to join two strings.

Example 2:
<html>
<body>
<p>Click the button to join two strings.</p>
<button onclick="myFunction()">Click</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str1 = "PHP ";
  var str2 = "Python ";
  var str3 = "Java";
  var res = str1.concat(str2,str3);
  document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
Output:

Click the button to join two strings.


No Sidebar ads