Join two or more strings using concat in Javascript

Published by admin on

javascript join two or more strings

Introduction :

Joining strings is called concatenation. In any programming language, you will have to write code for joining strings. Javascript strings are immutable, i.e. we can’t change the original string. If we want to modify the string, we need to create one new string using the characters of the given string. During string concatenation, one new string is created by joining all other strings.

Javascript provides one method called concat() to join multiple strings. We can join two or multiple strings using this method. In this tutorial, we will learn how to use the concat() method with different examples.

 

concat method syntax :

The syntax of concat() method is as below :

str.concat(str2[, str3, str4, str5......strN])

 

Parameters and return value :

It takes one or multiple parameters i.e. the strings to join with the origin string str. As explained above, a string is immutable. So, this method creates one new string joining all argument strings. The returned value is the newly created string. Remember that the returned string is a new string.

 

Example :

    let str1 = "Hello";
    let blank = " ";
    let str2 = "World";
    let str3 = "!!";
    
    let welcomeString = str1.concat(blank,str2,blank,str3);
    
    console.log(welcomeString);

 

Output :

It will print the below output :

Hello World !!

As you can see here, the strings are joined in the same order we are passing to the concat() method.

javascript string concat

 

Conclusion :

In this tutorial, we have learned how to concat strings in Javascript with an example. Try to go through the example shown above and drop one comment below if you have any queries.

Categories: javascript

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *