简体   繁体   English

我的代码在字符串和变量之间添加空格? 为什么这样做呢?

[英]My code is adding a space between the string and the variable? Why is it doing that?

So when I execute this code, it adds a space between names[i] and the string? 因此,当我执行此代码时,它会在name [i]和字符串之间添加空格?

var names = ["Bob", "Daniel", "John", "Jimmy", "Joseph"]

for (var i = 0; i < names.length; i ++) {
    console.log("I know someone called",names[i])
}

I'm new to this. 我是新来的。

That's just the behaviour of console.log . 那只是console.log的行为。 Each parameter you pass will be separated by a space, and is expected (see here ) - if you don't want the space, try: 您传递的每个参数都将以空格分隔,并且是预期的(请参阅此处 )-如果您不希望使用空格,请尝试:

console.log("I know someone called" + names[i]);

By using the + , you are directly concatenating the strings and ensuring there is no space 通过使用+ ,您可以直接连接字符串并确保没有空格

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

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