how to create line breaks in console.log() in node

Is there a way to get new lines in console.log when printing multiple objects?

Suppose we have console.log(a, b, c) where a, b, and c are objects. Is there a way to get a line break between the objects?

I tried console.log(a, '\n', b, '\n', c), but that does not work in Node.js.


Solution 1:

Add \n (newline) between them:

console.log({ a: 1 }, '\n', { b: 3 }, '\n', { c: 3 })

Solution 2:

I have no idea why this works in Node.js, but the following seems to do the trick:

console.log('', a, '\n', b, '\n', c)

Compliments of theBlueFish.

Solution 3:

Without adding white space at the start of a new line:

console.log("one\ntwo");

Output:

one

two

This will add white space at the start of a new line:

console.log("one", "\n", "two");

Output:

one

two

Solution 4:

An alternative is creating your own logger along with the original logger from JavaScript.

var originalLogger = console.log;
console.log = function() {
  for (var o of arguments) originalLogger(o);
}

console.log({ a: 1 }, { b: 3 }, { c: 3 })

If you want to avoid any clash with the original logger from JavaScript:

console.ownlog = function() {
  for (var o of arguments) console.log(o);
}

console.ownlog({ a: 1 }, { b: 3 }, { c: 3 })