10 Ways to Improve Your DevTools Console Logging

10 Ways to Improve Your DevTools Console Logging

Few developers delve vastitude the nuts of browser DevTool debugging. The unobtrusive console.log() is often derided but we all use it. It’s unconfined for outputting values as lawmaking runs and usually helps pinpoint errors.

Yet there is a range of under-utilized, quicker, easier, and increasingly useful options which can be used in client-side scripts, web workers, and service workers. Many are moreover supported in the Node.js and Deno runtime consoles.

Open your browser DevTools with F12, Ctrl|Cmd Shift I, or cmd option j and jump in.

1. Output variable names with ES6 destructuring assignment

Logging can wilt ramified when increasingly than one value is stuff monitored. It’s usually necessary to add remoter information, e.g.

const x = 42;

console.log('variableX:', variableX);

console.log(`variableX: ${ variableX }`);

A quicker option is to use ES6 object destructuring assignment. This adds the variable to an object with a matching property name. In other words, just place { and } brackets virtually a variable to show its name and value:

console.log({ variableX });

2. Use towardly log message types

console.log() is well-known:

console.log('no-frills log message');

but it’s not the only type. Messages can be classified as information (which is treated identically to console.log()):

1console.info('this is an information message');


console.warn('I warned you this could happen!');


console.error('I'm sorry Dear, I'm wrung I can't do that');

or less-important debugging messages:

console.debug('nothing to see here - please move along');

console.table() can output object values in a friendlier format:

const obj = {

   propA: 1,
   propB: 2,
   propC: 3
console.table( obj );


The table can be sorted into property name or value order by clicking an associated heading.

console.table() can moreover be used on each or multi-dimensional arrays:

onst arr1 = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
console.table( arr1 );

2.png Other options include:

  • console.dir( obj ) displays an interactive list of properties in a JavaScript object
  • console.dirxml( element ) displays an interactive tree of descendant elements from a specified HTML or XML node
  • console.clear() clears the panel of all previous messages.

3.Filter log messages

Browsers show log messages in towardly colors but they can moreover be filtered to exhibit explicit types. Chrome’s side bar is opened by clicking the icon at the top left of the Console pane:

Note that console.debug() messages are only shown when the verbose option is viewed.


4. Use printf-type messages

All log types can use C-style printf message formats which defines a template with % indicators where a variable is substituted. For example:

  'The wordplay to %s is %d.',
  'life, the universe and everything',

5. Log with style

Log messages can be styled using standard CSS passed as a string in a second parameter of any message type. A %c marker in the message indicates where the styling will be applied, e.g.


console.log('%cOK, You are super cool',
  `font-size: 2em;
  padding: 0.5em 2em;
  margin: 1em 0;
  color: $fff;
  background-color: #5FCB71;
  border-radius: 50%;`

The result in the DevTools console:


6. Use test-like assertions

The test-like console.assert() writ can be used to output a message when a condition fails. Assertions can be specified using a condition followed by one or increasingly objects to output when that condition fails, e.g.

  life === 42,
  'life is expected to be',
  'but is set to',

Alternatively, a message and substitution values can be used:

life === 42,
'life is expected to be %s but is set to %s',

Both options show an interjection error when the condition fails:


7. Run a stack trace

A log of all function calls that make up the current execution point can be output with console.trace():

unction callMeTwo() {
  return true;
function callMeOne() {
  return callMeTwo();
const r = callMeOne();

The trace shows which line made each undeniability and it can be tabular or expanded in the Console pane:


8. Group logging messages

Logging messages can be separated into named groups using console.group( label ) at the start and console.groupEnd() at the end. Groups of messages can be nested and tabular or expanded (console.groupCollapsed( label ) shows the group in its tabular state initially):

for (let i = 3; i > 0; i--) {
  for (let j = 97; j < 100; j  ) {



9. Use performance timers

The time( label ) writ starts a timer. The elapsed time in milliseconds is then reported when an associated timeEnd( label ) writ is reached. Timers can be used to assess the performance of an operation — it’s easier and increasingly well-judged than managing your own Date() calculations, e.g.


 for (let i = 999999999; i > 0; i--);



Up to 10,000 timers can be widow to a page and the console.timeLog( label ) writ will report the elapsed time without stopping the timer.

A similar option is console.count( label ) which reports the number of times the writ has been called. console.countReset( label ) resets the named counter when to zero.

10. Debug and monitor functions by name

The DevTools Sources panel (or Debugger in Firefox) allows you to unshut a file and set a breakpoint by clicking a line number. Chrome-based browsers moreover permit you to set a breakpoint by inward debug( functionName ) in the console, e.g.

debug( doSomething );

The function must be misogynist in the global namespace and the browser will launch the debugger as soon as it is called. Debugging can be cancelled using undebug( functionName ) or by reloading the page.

The monitor( functionName ) and its associated unmonitor( functionName ) commands are used in a similar way. Rather than halting execution, they log each undeniability to a function and show the passed arguments:

function doSomething tabbed with arguments: "hello", 2

Happy debugging, for modern frontend teams – Start monitoring your web app for free.

Browser DevTools have evolved from rudimentary consoles to sophisticated minutiae and debugging environments. console.log() will unchangingly be popular but the other options may offer a quicker and easier route to zero bugs!

Share this