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

2020-07-06 02:03发布

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

6条回答
倾城 Initia
2楼-- · 2020-07-06 02:42

Another way would be a simple:

console.log(a);
console.log(b);
console.log(c);
查看更多
欢心
3楼-- · 2020-07-06 02:43

I have no idea why this works in node but the following seems to do the trick:

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

compliments of theBlueFish

查看更多
Ridiculous、
4楼-- · 2020-07-06 02:53

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

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 JS

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

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

查看更多
家丑人穷心不美
5楼-- · 2020-07-06 02:55

Without adding white space at start of new line:-

console.log("one\ntwo");

output:-

one two

This will add white space at start of new line:-

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

output:-

one two

查看更多
ゆ 、 Hurt°
6楼-- · 2020-07-06 03:03

You need to use \n inside the console.log like this:

console.log('one','\n','two');
查看更多
▲ chillily
7楼-- · 2020-07-06 03:04

Add \n (newline) between them:

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

查看更多
登录 后发表回答