Skip to content

JavaScript concatenate string and variable | Simple example code

  • by

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.

<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.

<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:

JavaScript concatenate string and variable example

Do comment if you have any doubts and suggestions on this Variable and string topic of JS.

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

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