admin管理员组文章数量:1134247
Is it possible to print the output in the same line by using console.log()
in JavaScript? I know console.log()
always returns a new line. For example, have the output of multiple consecutive console.log()
calls be:
"0,1,2,3,4,5,"
Is it possible to print the output in the same line by using console.log()
in JavaScript? I know console.log()
always returns a new line. For example, have the output of multiple consecutive console.log()
calls be:
"0,1,2,3,4,5,"
Share
Improve this question
edited Feb 27, 2021 at 5:48
Aaron Morefield
99510 silver badges20 bronze badges
asked Feb 20, 2015 at 1:13
BBKayBBKay
6252 gold badges11 silver badges19 bronze badges
1
- 1 possible duplicate of Chrome JavaScript developer console: Is it possible to call console.log() without a newline? – ThreadedLemon Commented Feb 20, 2015 at 1:32
14 Answers
Reset to default 95In Node.js 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.
You could just use the spread operator ...
var array = ['a', 'b', 'c'];
console.log(...array);
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
So if you want to print numbers from 1 to 5 you could do the following:
var array = [];
for(var i = 1; i <= 5; i++)
{
array.push(i);
}
console.log(array.join(','));
Output: '1,2,3,4,5'
Array.join(); is a very useful function that returns a string by concatenating the elements of an array. Whatever string you pass as parameter is inserted between all the elements.
Hope it helped!
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")
You can also use the spread operator (...)
console.log(...array);
The "Spread" operator will feed all the elements of your array to the console.log
function.
You can also do it like this:
let s = "";
for (let i = 0; i < 6; i++) {
s += i.toString();
if (i != 5) {
s += ",";
}
}
console.log(s);
It's not possible directly in a browser environment. You'll need to create some buffer to hold values that you want to print on the same line.
In previous answers there are examples of using arrays and loops. As an alternative you can solve it functionally:
const print = ((buffer = '') => arg => {
if (arg !== '\n') {
buffer += arg
} else {
console.log(buffer)
buffer = ''
}
})()
print('x')
print('y')
print('z')
print('\n') // flush buffer
Or you can use object setters
const c = {
buffer: '',
set log(val) {
if (val !== '\n') {
this.buffer += val
} else {
console.log(this.buffer)
this.buffer = ''
}
}
}
c.log = 'foo'
c.log = 42
c.log = 'bar'
c.log = '\n'
You can use process.stdout.write
Like This in a loop..
process.stdout.write(`${arr[i]} `)
You can print them as an array
if you write:
console.log([var1,var2,var3,var4]);
you can get
[1,2,3,4]
You Can Use The Spread Operator. The "log" method Prints It's args in a Single Line and You can Give it The Elements of the Array as individual Arguments By Spreading the Array.
console.log(... array);
You can use comma delimiter:
console.log(1,2,3,4);
but if you want commas to show up in the output then you will need to use strings:
console.log('1,','2,','3,','4');
You can do this instead. It's simple.
var first_name = ["Ram", "Sita", "Hari", "Ravi", "Babu"];
name_list = "";
name_list += first_name;
console.log(name_list);
// OR you can just typecast to String to print them in a single line separated by comma as follows;
console.log(first_name.toString());
// OR just do the following
console.log(""+first_name);
// Output: Ram,Sita,Hari,Ravi,Babu
process.stdout.write('\x1b[2K');
process.stdout.write(`${anyDataOrText}` + '\x1b[0G');
\x1b[2K = remove last
\x1b[0G = put new
本文标签: javascriptPrint an output in one line using consolelog()Stack Overflow
版权声明:本文标题:javascript - Print an output in one line using console.log() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736857206a1955762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论