JavaScript concatenate string and int | Example code

You can directly add string and number in JavaScript, no need for a special method or typecasting. Just use a + (Adds two operands) OR += operator to Concatenate integer variable to a string variable.

+= is Add AND assignment operator, It adds the right operand to the left operand and assigns the result to the left operand

Example of JavaScript concatenate string and int

 <!DOCTYPE html> 
    <html> 
    <body> 
            <script language="JavaScript">
                var str = "Name";
                var num = 7; 
                str += num;
                alert(str);
            </script>     
    </body> 
    </html>

Here is a code of how to Concatenating numbers and String as a string in JavaScript.

<!DOCTYPE html> 
    <html> 
    <body> 
            <script language="JavaScript">
                var str = "Name";
                var num = 7; 
                var joint = str + num;
                alert(joint);
            </script>     
    </body> 
    </html> 

Output:

JavaScript concatenate string and int

Do comment if you have any suggestions and questions on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

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