41

I'm hoping to be able to pretty print array objects and such in the Console of Chrome DevTools. Is there any means to achieve this?

Thank you!

3
  • What do you mean by 'pretty print'? Things that appear in the console are being formatted by default (different font colours for different types, ability to expand objects and arrays etc.) Commented Aug 12, 2013 at 11:06
  • Hi @KonradDzwinel, I mean prettify output so that it's more easily read. But I'm not talking about the prettify button ({}) for the source files. But I mean when you have an array f.ex. and don't want it printed to to console as an inline array but stacked. Thank you! Commented Aug 12, 2013 at 12:33
  • DevTools have no settings to change the default console output. However you can override console.log with your own function. See my answer here: stackoverflow.com/questions/18178896/… Commented Aug 14, 2013 at 7:12

3 Answers 3

60

You could format the data as JSON:

console.log(JSON.stringify({foo:1, bar:2}, null, 4)); { "foo": 1, "bar": 2 } 
Sign up to request clarification or add additional context in comments.

1 Comment

Never knew that JSON.stringify(value[, replacer[, space]]) has more than one parameter... Thank you.
11

If you are at a breakpoint, you can call JSON.stringify() directly from the Chrome DevTools console:

> JSON.stringify(anObject, null, 2); <- "{ "field": "foo", "array": [ { "element": 1 }, { "element": 2 } ], "object": { "inner_field": "bar" } }" ----------------------------- > 

Comments

0

I write some years later and i am older too ... but i found this response trying to pretty print a block of code so i leave tracks of my workarounds.

Now, on december 2020, You can always open js objects in console log clicking on related arrows on the left.

Example:

inject example code in browser

console log

opened object in console

or you can use as stated before the JSON.stringify() method.

If you whant to pretty print a block of js code minimized in a long line add somewhere at the beginnig of code a debugger statement, than paste to the console and run thwe code. The Debugger statement will be reached and the code will be opened into "sources" panel. Here you can use the pretty print button. Pay attention, for your security, to put your debugger statement prior of each other executed statement.

Example:

((function(){/*AutoFill_LastPass*/_LPG=function(i){debugger; return document.getElementById(i);};_LPT=function(i){return document.getElementsByTagName(i);};if(_LPG('_lpiframe')){_LPG('_lpiframe').parentNode.removeChild(_LPG('_lpiframe'));}if(_LPG('_LP_RANDIFRAME')){_LPG('_LP_RANDIFRAME').parentNode.removeChild(_LPG('_LP_RANDIFRAME'));}_LASTPASS_INC=function(u,s){if(u.match(/_LASTPASS_RAND/)){alert('Cancelling_request_may_contain_randkey');return;}s=document.createElement('script');s.setAttribute('type','text/javascript');s.setAttribute('src',u);if(typeof(window.attachEvent)!='undefined'){if(_LPT('body').length){_LPT('body').item(0).appendChild(s);}else{_LPT('head').item(0).appendChild(s);}}else{if(_LPT('head').length){_LPT('head').item(0).appendChild(s);}else{_LPT('body').item(0).appendChild(s);}}};_LASTPASS_INC('https://lastpass.com/bml.php'+String.fromCharCode(63)+'v=0&a=0&r='+Math.random()+'&h=456d3ca99bf926f1727a6944fa06db246df102044140c09f0c9922b6ab1fa88a&u='+escape(document.location.href));_LPM=function(m){var targetFrame=_LPG(m.data.frame);if(null!=targetFrame&&typeof(targetFrame)!='undefined'&&typeof(targetFrame.contentWindow)!='undefined')targetFrame.contentWindow.postMessage(m.data,'*');};if(window.addEventListener){window.addEventListener('message',_LPM,false);}else{window.attachEvent('onmessage',_LPM);}var t=document.createElement('iframe');t.setAttribute('id','_LP_RANDIFRAME');t.setAttribute('sandbox','allow-scripts');t.frameBorder='0';t.setAttribute('src','https://lastpass.com/bml.php?u=1&hash=1&gettoken=0&donotcache=1407688374608280546');t.setAttribute('onload',"document.getElementById('_LP_RANDIFRAME').contentWindow.postMessage('ae24188b13eef4ddac2c37d1c449c47156d0a136c7db1d2ca0bd68060bffcc79','*');");if(typeof(window.attachEvent)!='undefined'){if(_LPT('body').length){_LPT('body').item(0).appendChild(t);}else{document.getElementByTagName('head').item(0).appendChild(t);}}else{if(_LPT('head').length){_LPT('head').item(0).appendChild(t);}else{_LPT('body').item(0).appendChild(t);}}})()); 

Result of running this code in console: example code stopped at breack point

pretty printed code

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.