Javascript Data Type How to - Join two strings with a comma and space between them








Question

We would like to know how to join two strings with a comma and space between them.

Answer


<!DOCTYPE html>
<html>
<body>
  <div id="demo"></div>
<!--   w  w w  .j a  v  a2  s  . co  m-->
<script type='text/javascript'>

var str1 = "Hello";
var str2 = "world!";
var result = str1.concat(", ").concat(str2);
document.getElementById("demo").innerHTML=result;

</script>
</body>
</html>

The code above is rendered as follows: