Michael Michael - 6 months ago 9
Javascript Question

How to make a space when concatenating strings

I have written some code that works fine but I am trying to make a space between the strings.

The code I have is:

<html>
<head>
<title> Concatenating Strings </title>
</head>
<body>
<script type = "text/javascript">
var greetingString = "Hello";
var myName = prompt ("Please enter your name","");
var concatString;
document.write(greetingString + "" + myName + "<br>");
concatString = greetingString + "" + myName;
document.write(concatString);
</script>
</body>
</html>


At the moment the script shows HelloMichael, I am wanting it to show Hello Michael. Can someone please advise me on how I can do that?

Answer

To insert a space character, simply insert a space character. For example:

greetingString + " " + myName

There's no space in your current output, because "" doesn't have a space in it.