How to console log better in JavaScript


You are probably already very familiar with using console.log() to print something to the console, especially the browser console. While this method is handy, there are lots of other methods on the console object that can make debugging, and our lives, for that matter, much easier.


Let's say we are retrieving a list of users from an api:

  .then((response) => response.json())
  .then((json) => console.log(json));

Our response looks like this:

Users response

We can display this data in a more readable table format with the following:

  .then((response) => response.json())
  .then((json) => console.table(json));

Now our response looks like this:

Users as table response

What if we are only interested in seeing specific pieces of data? We can pass an array to the console.table() method with the keys we are interested in, like so:

  .then((response) => response.json())
  .then((json) => console.table(json, ["username", "email", "phone"]));

Now our response looks like this:

Users as table response with specific data

console.log() - with custom styles

When logging something to the console, it can sometimes be hard to differentiate between what you are logging, and everything else. We can solve this by passing custom CSS to our console.log() to make it stand out.

console.log("%cTest", "background-color: green; padding: 10px");

Now our console.log() looks like this:

Console.log statement green


  "background-color: red; padding: 10px; border: 5px solid white; color: black;"

Looks like:

Console.log statement error red


This is another helpful method that allows you to navigate large JS objects better.

From the MDN docs:

The Console method dir() displays an interactive list of the properties of the specified JavaScript object. The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects.

In other words, console.dir() is the way to see all the properties of a specified JavaScript object in console by which the developer can easily get the properties of the object.


Looks like this:



Another simple method on the console object is console.error() this outputs a message formatted as an error.

console.error("there has been an error...");

Looks like this:



Another simple method on the console object is console.debug() this outputs a message formatted as a debug message.

console.debug("this is a debug message...");

Looks like this:



Another simple method on the console object is console.warn(); this will output a message formatted as a warning.

console.warn("this is a warning message...");

Looks like this:


Wrap up

You can find all the console methods over at the MDN Docs