Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print on same line using console.log() in JavaScript?

I want to print a list (var lst=[1,2,3,4,5]) using a loop on the same line. Can I do that in JavaScript ?

like image 519
Abhishek Agarwal Avatar asked Oct 19 '25 01:10

Abhishek Agarwal


2 Answers

Well you can achieve this by doing this. Make an empty string variable and then concatenate each array value to it, then print it out. This will work

var lst = [1, 2, 3, 4, 5];

var output = "";
for (var i = 0; i <= lst.length; i++){
   output += lst[i] + " ";
}

console.log(output);
like image 197
Ankit Kaushal Avatar answered Oct 21 '25 14:10

Ankit Kaushal


In NodeJS and as long as you didn't change your standard output you can use process.stdout.write("Something here") as the standard output of your application is terminal (console).

process.stdout.write("Hello ")
process.stdout.write("World")
process.stdout.write("!")

Hello world!

like image 40
Pouria Moosavi Avatar answered Oct 21 '25 14:10

Pouria Moosavi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!