Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Multiple Ways To Concatenate A string using JavaScript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 342
    Comment on it

    Hello, readers . In today's blog I am trying to give multiple ways to concatenate a string using JavaScript.

     

    Basically the term “Concatenate” means joining two or more strings and returns a new joined string .

     

    As there are various operators used in JavaScript for performing various Airthmetic calculations such as assigning and comparing the values.

     

    For concatenating a string , we can use following methods such as :-

    • The Addition operator ( + )
    • The Assignment operator ( += )
    • The built-in concat () method

    Example for each method is given below :-

     

    1. Concatenating a string using addition method :-

    html>
    
    <body>
    
    <script type="text/JavaScript">
    
    var str1 = "Welcome";
    
    var str2 = "To";
    
    var str3 = "JavaScript";
    
    var result = str1 +" "+ str2 +" "+ str3;
    
    document.write(result);
    
    </script>
    
    </body>
    
    </html>

    In this code , I have created a string with a variable name as str1 ,str2 , str3 and assign values to it.

     

    Now for the result, I have created a variable with a result name and had added all three strings in it.

     

    Now on calling the result variable , we will get the desired result.

     

    2. Concatenating a string using Assignment operator ( += ):-

    <html>
    
    <body>
    
    <script type = "text/javascript">
    
    var str1 = "Welcome";
    
    str1 += " To";
    
    document.write(str1);
    
    </script>
    
    </body>
    
    </html>

     

    In this code , I have created a variable with name as srt1 and assigned a value to it.

     

    Now to the string , I have used addition assignment operator that adds the value of the right operand to a variable and hence, assigned the result to the created variable.

     

    Now on calling the srt1 variable, we will get the desired result.

     

    3. Concatenating a string using Concatenate Method

    <html>
    
    <body>
    
    <script type="text/JavaScript">
    
    var str1 = "".concat("Welcome ","To ","Javascript");
    
    document.write(str1);
    
    </script>
    
    </body>
    
    </html>


    Now , in this code for  Concatenating a string , we will use concat() method for adding the strings.


    Now on calling the created srt1 variable , we will get the desired result.


    Conclusion:-


    Hence , I have explained the multiple ways of  Concatenating a string using JavaScript with the help of various operators.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: