AppDividend
Latest Code Tutorials

Javascript String Concat Example | String.prototype.concat() Tutorial

0

Javascript String Concat Example | String.prototype.concat() Tutorial is today’s leading topic.  The concat() method concatenates string arguments to a calling string and returns a new string. The concat() method is used to one or more strings. The concat() method does not change existing strings but returns a new string containing the text of the joined strings.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript String Concat Example

Javascript String concat() function concatenates the string arguments to a calling string and returns the new string. The changes to an original string or the returned string don’t affect the other. This function returns a new string that is the combination of all the different strings passed to it as the argument. The syntax is javascript string concat() syntax is following.

string.concat(string1, string2, ..., stringX)

Minimum one string parameter is required to concat a string with another string. The number of arguments to this function is equal to the number of strings to be joined together. Let us take an example. 

// app.js

let hello = 'Good Morning, ';
console.log(hello.concat('MJ', '. Have a nice day.'));

 

Javascript String Concat Example | String.prototype.concat() Tutorial

If the arguments are not of a type string, they are converted to string values before concatenating.

One test concludes that the assignment operators (+) can be used in string concatenation instead of using the string concat() method.

Concat three strings

We can write the following code to concat three strings.

// app.js

let str1 = 'Harry, ';
let str2 = "Hermione, and";
let str3 = " Ronald";
let res = str1.concat(str2, str3);
console.log(res);

We can also do the same thing with the + operator.

// app.js

let resplus = str1 + str2 + str3;
console.log(resplus);

The output will be the same for this example, but we have used the + operator instead of the concat method.

 

Concat three strings

Finally, Javascript String Concat Example | String.prototype.concat() Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.