Search code examples
javascripthtmlpopupspace

space between words in JS


I have this JS code:

function myPopUp()
{
    var y;
    var name=prompt("Please enter your name"," הכנס את שמך");
    if (name!=null)
    {
        y= '  How are you today?';
        document.getElementById("popup").innerHTML = y;
        var x="";
        var time=new Date().getHours();
        if (time<20)
        {
            x="Have a nice day" +  name + '!';
        }
        else
        {
            x='   Great evening  '+ name +'   !  ';
        }
        document.getElementById("demo").innerText = x ;
    }
}

with this HTML code:

<body>
    <button onclick="myPopUp()" >לחץ כאן</button>
    <p id="demo"></p>
    <p id="popup"></p>
</body>

My question: When someone write their name there is no space between the words? How do I put a space between words in JS?

Thank you.


Solution

  • Looks like you just need a space after "day," so try this:

    x="Have a nice day " +  name + '!';