JavaScript > String Methods

String Concat in JavaScript

How to concatenate strings in JavaScript?


Concat

Concat is an string method in JavaScript, which is used to join tow or more number of strings and returns the new string after joining. The variables with string values are the required parameters of concat() method.

In JavaScript, by using concat() method we can join the strings. 

Joining the strings to form the name of the website.

<p>Click on the "Button" to join the three strings.</p>
<input type="button" value="Click to Join" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {    //This is a String//
        var a = "Tech";      //This is a String//
        var b = "Funda";     //This is a String//
        var c = ".com";
        var res = a.concat(b).concat(c);
        document.getElementById("myId").innerHTML = res;
    }
</script>

In the above code snippet we have given ID as "myId" to the second <p> element and we have given three strings in the script code. First string is "Tech" to the variable "a", second string is "Funda" to the variable "b", third string is ".com" to the variable "c". For joining these three strings we have given a.concat(b).concat(c) to the result variable. Onclick of the button "Click to Join" fires the Function() in the script code and joins the three strings in the output.

OUTPUT

Joining the strings to form the sentence.

<p>Click on the "Button" to join the three strings.</p>
<input type="button" value="Click to Join" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {    //This is a String//
        var a = "Mr. ";      //This is a String//
        var b = "Sheo ";     //This is a String//
        var c = "Narayan";   //This is a String//
        var d = " is a Software"; //This is a String//
        var e = " Professional." //This is a String//
        var res = a.concat(b, c, d, e);
        document.getElementById("myId").innerHTML = res;
    }
</script>

Joining to form sentence

We need to join all the strings in the script code, for that we are using concat() method. Onclick of the button joins the strings and returns the output.

OUTPUT

 Views: 1080 | Post Order: 69



Write for us