Karl Morrison Karl Morrison - 4 months ago 8
Javascript Question

console.log %o not printing object argument

According to https://developer.mozilla.org/en-US/docs/Web/API/Console the term

%o
should print out an argument as an object.

The following code:

var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];
console.log('myFunc(%o)', a);


Results in the output:

myFunc(%o) [ 34, 203, 3, 746, 200, 984, 198, 764, 9 ]


I was expecting:

myFunc([ 34, 203, 3, 746, 200, 984, 198, 764, 9 ])


Workaround: Since %o is not a standard as Tusher pointed out and does not exist in node https://nodejs.org/api/console.html the following made the correct output:

console.log('myFunc([%s])', a);


robertkleps version also works:

console.log('myFunc(%j)', a)

Answer

Using es6 template literals:

var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];
console.log(`myFunc([${a}])`);

> myFunc([ 34, 203, 3, 746, 200, 984, 198, 764, 9 ])

Hope it helps :)