简体   繁体   English

JavaScript:在两个字符串之间添加空格vriable为什么不按两个字符串顺序

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

1) Using variable: 1)使用变量:

var str1 = "Hi";

var str2 = "Geeks!";

document.write(str1+str2);

OUTPUT: 输出:

Hi Geeks! 嗨,怪胎!


2) Using Quotes 2)使用引号

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

OUTPUT 输出值

HiGeeks! 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. 谢谢。

You are saying that in the following code (1) print 您是说在下面的代码(1)中打印

Hi Geeks!

And (2) 和(2)

HiGeeks!

The former with a space and the latter without it? 前者有空间而后者没有空间? jsfiddle 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! 有一个!

read the documentation of js 阅读js的文档

and the str1 + str2 will give you "helloworld"

to concat two strings with space 用空格连接两个字符串

var str1 = "hello";
var str2 = "world";

console.log(str1 +' '+ str2)

'+' is used to concat two strings '+'用于连接两个字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM