Search code examples
javascriptstringfunctionconcatenationstring-concatenation

What is the purpose of first parameter in Javascript concat() method?


Consider the following code example demonstrating the usage of concat() method (Taken from W3Schools Javascript Tutorial):

<!DOCTYPE html>
<html>
  <body>

    <h2>JavaScript String Methods</h2>

    <p>The concat() method joins two or more strings:</p>

    <p id="demo"></p>

    <script>
    var text1 = "Hello";
    var text2 = "World!";
    var text3 = text1.concat(" ",text2);
    document.getElementById("demo").innerHTML = text3;
    </script>

  </body>
</html>

The output of above program is :

JavaScript String Methods

The concat() method joins two or more strings:

Hello World!

Now, if I want to concat the two words "Hello" and "World!" with a blank space added in between them I can make add a space at the end of the word "Hello " or at the beginning of the word " World!".

If above is the case then how should I do it using the concat() method? Do I still need to provide a blank space as first parameter in concat() method? Please explain me "What is the actual purpose of first parameter in concat method"? Also, explain me it is necessary?

Thanks.


Solution

  • String.concat(str1, str2, str3, ...., strN)

    is accepting N number of parameter. This means it will glue all your parameters to one single string.