To make a string out of a string and a passed variable in JS use concatenation to combine the variable to another string.
Simply just add a plus sign+ between the strings or string variables you want to connect.
1 2 3 4 |
<script> let animal = 'Horse'; console.log('My favorite animal is the ' + animal + '.'); </script> |
JavaScript concatenate string and variable Example
HTML example code:-
String and String variable or String and integer variable both are concatenated same way in JavaScript.
1 2 3 4 5 6 7 8 9 10 11 12 |
<html> <body> <script> let animal = 'Horse'; console.log('My favorite animal is the ' + animal + '.'); var day = "6" console.log("Today day is " + day); </script> </body> </html> |
Note: You can use a single quote or a double quote it doesn’t matter as long as your opening and closing quotes are the same.
Output:

Do comment if you have any doubts and suggestion on this Variable and string topic of JS.
Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version