admin管理员组文章数量:1134247
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.
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.
12 Answers
Reset to default 60Add \n
(newline) between them:
console.log({ a: 1 }, '\n', { b: 3 }, '\n', { c: 3 })
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)
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
You can use a template literal:
console.log(`a is line 1
b is line 2
c is line 3`)
To activate a template literal, you need to click on the character to the left of number 1 (on my keyboard - Microsoft Wired 600).
Do not confuse: The '
symbol is the same as Shift + @ on my keyboard with `
.
'
and "
will create strings and `
will create template literals.
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 })
Using one call avoids code duplication -> no multiple calls
Using a template literal works, but is sensitive to formatting -> no template literal
I guess browsers pick their own way to delimit arguments, and there is no way to specify the delimiter. It seems to be a space. -> no specify '\n' as delimiter
After considering these solutions, we arrive at what @Vasan has pointed out in a comment - console
has powerful printf-style formatting capabilities.
const s1 = "foo";
const s2 = "bar";
const a = { a: 1 };
const b = { b: 2 };
console.info("Remove space delimiter");
console.log("%s%s", s1, s2);
console.info("Separate with newline");
console.log("%s\n%s", s1, s2);
// Interactive object and label
// See https://developer.mozilla.org/en-US/docs/Web/API/console/log#logging_objects
console.info("Interactive object and label");
console.log(
"%s\n%o",
"`a` object",
a,
);
// Object snapshot and label
// See https://developer.mozilla.org/en-US/docs/Web/API/console/log#logging_objects
console.info("Object snapshot and label");
console.log(
"%s\n%o",
"`a` object is",
JSON.parse(JSON.stringify(a)),
);
console.info("Multiple objects *");
console.log("%o\n%o", a, b);
* Awkwardly, in Firefox, it seems that objects that are not the first nor last replacement argument have spaces on both sides no matter what. This does not seem to be the case in Chrome.
See console and particularly console string substitutions.
Aside
console output can even be styled using some CSS properties.
See https://gitlab.com/users/sign_in for an example of this.
See https://developer.mozilla.org/en-US/docs/Web/API/console#styling_console_output
Another way would be a simple:
console.log(a);
console.log(b);
console.log(c);
Just do console.log({ a, b, c });
Example:
var a = 'var a';
var b = 'var b';
var c = 'var c';
console.log({ a, b, c });
This will add an expandable area in the console:
You need to use \n
inside the console.log
like this:
console.log('one','\n','two');
With backticks, you can simply add variables and "\n":
const a = {k1: 'key1', k2: 2};
const b = "string";
const c = 123;
console.log(`${JSON.stringify(a)}\n${b}\n${c}`);
will log:
{"k1":"key1","k2":2}
string
123
Try:
const a = {k1: 'key1', k2: 2};
const b = "string";
const c = 123;
console.log(`${JSON.stringify(a)}
${b}
${c}`);
`` takes multiline input and produce output with multilines.
New line character:
console.log("\n");
You can try this:
console.log(a + "\n" + b + "\n" + c);
本文标签: javascriptHow to create line breaks in consolelog() in NodejsStack Overflow
版权声明:本文标题:javascript - How to create line breaks in console.log() in Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736837023a1954934.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
console.log(a); console.log(b); console.log(c)
works as well – axiac Commented Apr 4, 2018 at 21:03