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);
}
Right now it's
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
)?
If you mean multiple console. log() outputs to a single line, we can't. It always starts on a new line. The only way is to combine everything into a single string, then log it all in one console.
In that case, we can use process. stdout. write() method to print to console without trailing newline. Note: The process object is global so it can be used without using require() method.
In Node.js you can also use the command:
process.stdout.write()
This will allow you to avoid adding filler variables to your scope and just print every item from the for loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With