循环控制台打印的Javascript在一行中

2022-01-11 00:00:00 loops for-loop console printing javascript

我正在尝试从我的 for 循环中获取输出以在控制台中的单行中打印.

I'm trying to get the output from my for loop to print in a single line in the console.

for(var i = 1; i < 11; i += 1) {
    console.log(i);
}

现在是

1
2
3
4
5
6
7
8
9
10

如何在一行中获得所有输出(像这样1 2 3 4 5 6 7 8 9 10)?

How can I get the output all in one line (like this 1 2 3 4 5 6 7 8 9 10)?

推荐答案

构建一个字符串,然后在循环之后记录它.

Build a string then log it after the loop.

var s = "";
for(var i = 1; i < 11; i += 1) {
  s += i + " ";
}
console.log(s);

相关文章