1.

What are cookies in JavaScript?

Answer»

To JOIN two or more strings, use the JavaScript concat() method. A new string is returned with the JOINED strings.

The syntax:

string.concat(str1, str2, str3..., strn)

Here, str1, str2, and str3 are the strings you want to join.

Let’s say the following are our two strings to be joined:

var myStr1 = "One"; var myStr2 = "Time!";

Now, use the concat() method to join both the strings:

var res = myStr1.concat(myStr2);

The example DEMONSTRATES how to join two strings:

<!DOCTYPE html> <html> <body> <button ONCLICK="show()">Joined Strings</button> <p id="myid"></p> <script> function show() {a     var myStr1 = "One";     var myStr2 = "Time!";     var res = myStr1.concat(myStr2);     document.getElementById("myid").innerHTML = res; } </script> </body> </html>

The output:

Let us now see another example wherein we will join three strings. Now we have three strings:

var myStr1 = "Only"; var myStr2 = " One"; var myStr3 = " Time!";

Join three strings with the concat() method but with an extra parameter for the 3rd string:

myStr1.concat(myStr2, myStr3);

The example that joins three strings:

<!DOCTYPE html> <html> <body> <button onclick="show()">Joined Strings</button> <p id="myid"></p> <script> function show() {     var myStr1 = "Only";     var myStr2 = " One";     var myStr3 = " Time!";     var res = myStr1.concat(myStr2, myStr3);     document.getElementById("myid").innerHTML = res; } </script> </body> </html>

The output displays the result of three strings in a new string on the click of a button:



Discussion

No Comment Found