Subash Sci Subash Sci - 3 months ago 7
Javascript Question

JavaScript : White space adding between two string vriable why not in sequence of two strings

1) Using variable:

var str1 = "Hi";

var str2 = "Geeks!";

document.write(str1+str2);


OUTPUT:

Hi Geeks!




2) Using Quotes

document.write("Hi"+"Geeks!");


OUTPUT

HiGeeks!




But,in a sequence made of strings if I use quotes I don't have any space between concatenation of two strings.. why?
Thanks.

Answer

You are saying that in the following code (1) print

Hi Geeks!

And (2)

HiGeeks!

The former with a space and the latter without it? jsfiddle

<head>
    <script src="script.js"></script>
</head>

<body>
    <script>
        var str1 = "Hi";
        var str2 = "Geeks!";

        document.write(str1 + str2); //(1)
        document.write("<br>");
        document.write("Hi" + "Geeks!"); //(2)

    </script>
</body>

</html>

There is no space between them, if you write this:

document.write("Hi" + " Geeks!");

or this:

document.write("Hi " + "Geeks!");

or this:

document.write("Hi" + " " + "Geeks!");

there is one!

Comments