Print output of the same line using console log in javascript

My question is, is it possible to output the output on the same line using console.log in JavaScript? I know console.log is always a new line. For instance:

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

Thanks in advance!

+11
source share
6 answers

Could you just put them in one 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 
+4
source

in nodejs there is a way:
process.stdout
so this might work:
process.stdout.write('${index},');
where: index - current data, and , - separator
You can also check the same topic here.

+26
source

You can just use the distribution operator ...

 var array = ['a', 'b', 'c']; console.log(...array); 

+6
source

You can just console.log lines all in one line:

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

To create a new line, use \n :

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

If you run your application on node.js, you can use ansi escape code to clear the line \u001b[2K\u001b[0E

 console.log("old text\u001b[2K\u001b[0Enew text") 
+3
source

Therefore, if you want to print numbers from 1 to 5, you can 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 (); This is a very useful function that returns a string by combining array elements. Whatever string you pass as a parameter, it is inserted between all elements.

Hope this helps!

+2
source

You can print them as an array

if you write:

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

you can get

 [1,2,3,4] 
0
source

Source: https://habr.com/ru/post/982763/


All Articles