How to create line breaks in console.log() 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.

7

12 Answers

Add \n (newline) between them:

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

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.

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

1

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

Another way would be a simple:

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

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

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

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

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.

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

New line character:

console.log("\n");

You can try this:

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

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
console.info("Interactive object and label");
console.log( "%s\n%o", "`a` object", a,
);
// Object snapshot and label
// See
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 for an example of this.

See

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like