printing output same line using console log in jav

2019-03-19 05:05发布

I have a question that is it possible to print the output in the same line by using console.log in JavaScript? I know console.log always a new line. For example:

"0,1,2,3,4,5,"

Thanks in advance!

5条回答
再贱就再见
2楼-- · 2019-03-19 05:43

in nodejs there is a way:
process.stdout
so, this may work:
process.stdout.write(`${index},`);
where: index is a current data and , is a delimiter
also you can check same topic here

查看更多
SAY GOODBYE
3楼-- · 2019-03-19 05:44

You can print them as an array

if you write:

console.log([var1,var2,var3,var4]);

you can get

[1,2,3,4]
查看更多
姐就是有狂的资本
4楼-- · 2019-03-19 06:04

Couldn't you just put them in the same call, or use a loop?

var one = "1"
var two = "2"
var three = "3"

var combinedString = one + ", " + two + ", " + three

console.log(combinedString) // "1, 2, 3"
console.log(one + ", " + two + ", " + three) // "1, 2, 3"

var array = ["1", "2", "3"];
var string = "";
array.forEach(function(element){
    string += element;
});
console.log(string); //123
查看更多
forever°为你锁心
5楼-- · 2019-03-19 06:04

You can just console.log the strings all in the same line, as so:

console.log("1" + "2" + "3");

And to create a new line, use \n:

console.log("1,2,3\n4,5,6")

If you are running your app on node.js, you can use an ansi escape code to clear the line \u001b[2K\u001b[0E:

console.log("old text\u001b[2K\u001b[0Enew text")
查看更多
萌系小妹纸
6楼-- · 2019-03-19 06:05

You could just use the spread operator ...

var array = ['a', 'b', 'c'];

console.log(...array);

查看更多
登录 后发表回答