Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to console.log without a newline?

I want to console.log my array like this without newlines:

const myarr = [ 1, 2, 3, 4, 5 ];

myarr.forEach((e) => console.log(e));

Actual result:

1
2
3
4
5

Desired result:

1 2 3 4 5

or

12345
like image 689
AA Sog Avatar asked Oct 21 '25 16:10

AA Sog


2 Answers

You could spread the array. Then all values are taken as parameters.

let array = [1, 2, 3, 4, 5];

console.log(...array);
like image 193
Nina Scholz Avatar answered Oct 23 '25 06:10

Nina Scholz


To answer the actual question, there are two possible answers:

  1. If you are running in a browser, then by default, no, but according to this answer from Chrome JavaScript developer console: Is it possible to call console.log() without a newline? you can by making a virtual console on top of the browser's console.
  2. If you're using node, then you can use process.stdout.write(msg) from this answer Chrome JavaScript developer console: Is it possible to call console.log() without a newline?
like image 39
iggy12345 Avatar answered Oct 23 '25 07:10

iggy12345



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!