I recently found out you can add colors and basic CSS to console.log()
calls with the use of the %c
character. (Learned about it from this answer Colors in JavaScript console)
It works well for string
values. However, I tried logging an array, and got a pretty disappointing result. It logged it as [object Object]
, instead of giving me an interactive view of the array, and the ability to expand it, so I could look at the objects inside.
I am aware you can use JSON.stringify
to see ALL the content, but I want the default functionality of console.log
so I can fold and expand at will, to avoid clutter in the console.
Is there a solution for this?
Code I have used:
console.log(`%csuccess`, 'background-color: #2FA745; color: #fff; display: block; padding: 4px;');
const myArray = [{test: 'hi'},{test2: 'hello'}];
console.log(`%c${myArray}`, 'background-color: #2FA745; color: #fff; display: block; padding: 4px;');
console.log('default behaviour of array', myArray);