Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript for loop console print in one line

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)?

like image 951
CH-SO Avatar asked Oct 12 '15 20:10

CH-SO


People also ask

How do I print multiple console logs in the same line?

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.

How do you print to the console without a trailing newline?

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.


1 Answers

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.

like image 122
user11073489 Avatar answered Oct 14 '22 22:10

user11073489