How to create line breaks in console.log() in Node.js

Javascriptnode.js

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

Add \n (newline) between them:

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

Solution 2 - Javascript

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 - Javascript

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 - Javascript

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.

Solution 5 - Javascript

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 })

Solution 6 - Javascript

Another way would be a simple:

console.log(a);
console.log(b);
console.log(c);

Solution 7 - Javascript

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

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

Solution 8 - Javascript

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:

Enter image description here

Solution 9 - Javascript

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.

Solution 10 - Javascript

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

Solution 11 - Javascript

New line character:

console.log("\n");

You can try this:

console.log(a + "\n" + b + "\n" + c);

Solution 12 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDCRView Question on Stackoverflow
Solution 1 - JavascriptOri DroriView Answer on Stackoverflow
Solution 2 - JavascriptDCRView Answer on Stackoverflow
Solution 3 - JavascriptShraddha JView Answer on Stackoverflow
Solution 4 - JavascriptfruitloafView Answer on Stackoverflow
Solution 5 - JavascriptEleView Answer on Stackoverflow
Solution 6 - JavascriptSua MoralesView Answer on Stackoverflow
Solution 7 - JavascriptmanishkView Answer on Stackoverflow
Solution 8 - JavascriptAlexander GotfridView Answer on Stackoverflow
Solution 9 - JavascriptSombir KumarView Answer on Stackoverflow
Solution 10 - JavascriptEduardo TomassiView Answer on Stackoverflow
Solution 11 - JavascriptMadhuka DilhanView Answer on Stackoverflow
Solution 12 - JavascriptDersView Answer on Stackoverflow