All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Concatenate String And Variable

Last Updated : Jul 1, 2023

JavaScript Concatenate String And Variable

In this tutorial we will show you the solution of JavaScript concatenate string and variable, we defined two variables for stores string and integer type values and we concatenated those variables with string by javascript support.

When we need to do this we have to use ‘$’ before variable name and define variable name between curly braces that denotes the variable then result printed on console.

Step By Step Guide On JavaScript Concatenate String And Variable :-

Here we defined variable ‘v’ with integer value, variable ‘str’ with string value ‘Everyone!’.

Now we have to bind that variables ‘v, str’ with specific string for that we need to specify $ symbol before variable name and insert variable name between curly braces.

Those are mainly define within ‘`’ quotes otherwise it throws error. Then we printed results on console by console.log() method.

<!DOCTYPE html>
<html>
    <head>
        <title>Concatenate string and variable</title>
    </head>
    <body>
        <script>
            var v = 242;
            var str = "Everyone!";
            var res1= `Number ${v}`;
            var res2= `Hi ${str}`;
            console.log(res1);
            console.log(res2);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In script we defined variables ‘v’ for store ‘242’, ‘str’ for store ‘Everyone!’ and for use those variable with string we need to follow above seen rules
  7. So we `Number ${v}` defined like this, here ${v}-refers ‘242’ then we concatenated this with string ‘Number’ and result stored on variable ‘res1’.
  8. We ` Hi ${str}` defined like this, here ${str}-refers ‘Everyone!’ then we concatenated this with string ‘Hi’ and result stored on variable ‘res2’.
  9. At last we printed results of ‘res1, res2’ on console using console.log(). In script we know that for print on console we need to use console.log() method and for open console panel right click on window tab select ‘inspect’ then panel will open there you have to click console tab.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to concatenate string with variable in javascript.

When we executes program on browser we can’t see the result because we printed on console panel so we have to use shortcut ‘ctrl+shift+j’ then console panel will open at right hand side with results of ‘Number 242, Hi Everyone!’.

If your need to change above defined variables with some other values then you can modify it in this concept accordingly.

I hope this tutorial on JavaScript concatenate string and variable helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪