Become Super Productive with Console.X
Tags: webdev, javascript, productivity
Published: 2021-07-24 13:11:36

The better you know the machine, the better you can use it!

Yes or no?

Whether you make your living in a blue-collar or white-collar career, you must always have access to the right tools you need to do your job. Otherwise, not only will you not be able to perform in the ways you need to, but you also won’t have as positive a relationship with your work.

> “A man is only as good as his tools.”
>
> ― Emmert Wolf

Developers debugging console has been available in one form or another in web browsers for many years.

Starting as a means for errors to be reported to the developer, its capabilities have increased in many ways, such as automatically logging information like network requests, network responses, security errors, or warnings.

There is more to the console object than just `console.log()`. Though the console object has got a shedload of features — very few get explored.

In this article, We will unravel the console object — things that remain hidden for most of the developers.

# Come, let us sit at the table and have some conversation!

You might have put an array or object inside a `console.log()`many times.
But do you know that there’s another option for these data types for a more structured display, which is the `console.table()`?



![console.table](https://miro.medium.com/max/1210/1*YrG-PQFFE3yncV4XGNMGeQ.png)

> Be patient, the tables will turn.
>
> (And if they won’t, make them turn.)
>
> — The Ruhaniyat

![console.table](https://miro.medium.com/max/1728/1*K81Qh_p9XcZ3wM1q-zWtww.png)

This looks much slick, isn’t it?

# Time to add some flavors to your life.

JavaScript gives us are five options to log messages.

**console.log( ) | console.info( ) | console.debug( ) | console.warn( ) | console.error( )**

> “People try to hold onto the sameness. This holding onto prevents growth.”
>
> — Bruce Lee

At first glance they seem to do the same thing and technically, they do! The browsers provide additional features tied to the five commands to give each their own distinct benefit.



![](https://miro.medium.com/max/1210/1*90dcqR68dQ6CePZxU-reqQ.png)



![](https://miro.medium.com/max/1210/1*ZG2XfQ-4m3OWOHvpkhxSWw.png)

The intentions for the usage of ***error*** and ***warn*** are easy to determine — how to use the other choices is up to you.

Don’t just write better error messages, write code that doesn’t need them.

# Darling, you need to add some colour to your life.

Wouldn’t it be amazing if the errors come up in red and warnings in yellow?

Is it possible? Definitely!

You can apply CSS styles to the console’s output. It works just like the string substitution method where you insert a `%c` variable for styles to be applied from the parameters.


![](https://miro.medium.com/max/1210/1*UgWCpjbUrmmG8JEsJD9STw.png)

Notice the %c before the I in the above statement.



![](https://miro.medium.com/max/1210/1*XyE8DWFrVA2gWcM-PBTJ2Q.png)

> “Colour to the eye what music is to the ear.”
>
> — Louis Comfort Tiffany

This is just a sample, you can achieve a lot of things using CSS. If you are interested then, [have a look at this thread.](https://stackoverflow.com/questions/7505623/colors-in-javascript-console#:~:text=Use%20Markdown%20for%20log%20messages.&text=The%20CSS%20format%20specifier%20allows,apply%20as%20the%20second%20parameter.)

# Memories of a past love — C Programming.

In C programming there is something called **placeholders**. These placeholders are also called **format specifiers**. It determines the **format** of the input and output.

Not many know this but JavaScript also provides us placeholders. We can make use of those placeholders in JavaScript’s `console.log()`


![](https://miro.medium.com/max/1210/1*Ya1-cyt8ZInp--Hm4rTQmw.png)


![](https://miro.medium.com/max/1210/1*-xQhpR_ZFPTwU4c8-PQwYA.png)

Notice the %s and %o that we have used in the console.log()


![](https://miro.medium.com/max/1210/1*kvnQbW_yMSJHSgqXhJp2fw.png)

# Can You Describe that thing a Little Better?

When something is descriptive it gives us a better idea of what that thing possibly would be.

The `console``**.**``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.



![](https://miro.medium.com/max/1210/1*mM-cvD0Z8tDpgmymrSwPBg.png)



![](https://miro.medium.com/max/1210/1*fuM5ODtr8rkJCwo0MenNIQ.png)

console.dir() didn’t just give an output but also told what kind it is.

Things get interesting is when we start using DOM nodes. So let’s look at the difference when we use `dir` and `log` on `document.body`



![](https://miro.medium.com/max/1210/1*0X8m1GAoxqPg1lRG5TUhrw.png)

Notice, `console.log()` prints the element in an HTML-like tree and `console.dir()` prints the element in a JSON-like tree.


![](https://miro.medium.com/max/1210/1*QwOf8Fg4KmcVjxKNSyLkxg.gif)

`console.log()` gives special treatment to DOM elements, whereas `console.dir()` does not. This is often useful when trying to see the full representation of the DOM object.

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

# Alone we are strong, together we are stronger!

A **group** is a set of things that are considered because they have something in common. It is quite possible to group the messages with the console.

Using `console.group()` we can group different console outputs together to show a form of relationship among them.



![](https://miro.medium.com/max/1210/1*fAkV9c9XdzBwY8LPPpX-pw.png)

We use group. End() to close the group.



![](https://miro.medium.com/max/1210/1*InR_w1a5KdIUL_6qwiMiQg.png)

# You can count on me like 1,2,3 & I’ll be there!

Man, even in the lower stages of development, possesses a faculty which, for want of a better name, I shall call Number Sense. This faculty permits him to recognize that something has changed in a small collection when, without his direct knowledge, an object has been removed from or added to the collection.

> “Many of the things you can count, don’t count. Many of the things you can’t count, really count.”
>
> **— Albert Einstein**

Ever wonder how many times a certain thing happens in your code? For instance, how many times does a particular function get called during a sequence of events? That’s where the `console.Count()` command can help out.



![](https://miro.medium.com/max/1210/1*hR4lR880a-MvxL2flBd8uA.png)



![](https://miro.medium.com/max/1210/1*Rf8khnjIaAVZi6Sj9_TMKA.png)

# To know oneself, one should assert oneself.

In life, truth is the only authority. One must reject everything but not the truth.

> It is truth that liberates, not your effort to be free.
>
> — J. Krishnamurti

Ever thought of logging something when it is not true?

`console.assert()` comes quite handy when you only want to print some selected logs i.e. it will only print the false argument. It does nothing at all if the first argument is true.


![](https://miro.medium.com/max/1210/1*7uoIfSdG_gI8ALe3ht8Kvw.png)



![](https://miro.medium.com/max/1210/1*JSYtY1DH3dLTZx56f0kCtA.png)


![](https://miro.medium.com/max/1210/1*gA9i-K-D1BH0ddHxYMH1_A.png)

# Are you tracing it?

Bugs can be a source of frustration. It’s impossible to know straight away which call to the function is creating the bug. Understanding the code execution flow, one can have a better grip on them.

You can use `console.trace()` to get a quick and easy stack trace to better understand code execution flow.


![](https://miro.medium.com/max/1210/1*xRboBdFALSZK88C9fVehew.png)


![](https://miro.medium.com/max/1210/1*U3U1ExVcxSWPlrFVMZCjSA.png)

In simple terms, `console.trace()` will log to the console the call stack that exists at the point `console.trace()` was called.

`console.trace()` can be very helpful especially, in a large codebase where we have established that the code has a bug in it somewhere but, we aren’t sure exactly where it is.

# Do you realize that’s the first time you’ve done that?

The [majority of code execution time is spent within loops](https://javascript.plainenglish.io/the-secrets-surrounding-for-loops-in-javascript-3db58541caa9?source=friends_link&sk=665c05b53d48c8d6316f4fc3df107be1). Understanding the performance impact of loops in JavaScript is essential in becoming a better developer.

Using `console.time()` one can get an idea of how long a certain section of code is taking.


![](https://miro.medium.com/max/1210/1*hzuLAyoMO3HpYmo3edbZ-g.png)


![](https://miro.medium.com/max/1210/1*sEE4QRBY-ijPXEJc7FKuTw.png)

> “Time is a created thing. To say ‘I don’t have time’, is like saying, ‘I don’t want to.”
>
> ― **Lao Tzu**

When I covered the [performance of loops in an article](https://javascript.plainenglish.io/the-secrets-surrounding-for-loops-in-javascript-3db58541caa9?source=friends_link&sk=665c05b53d48c8d6316f4fc3df107be1), I found that the [**Performance Interface**](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now)  gives more [accurate results](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now).

Learn to see the console object as a multifaceted tool. Each tool is designed precisely for a specific purpose. Choosing the correct tool will decrease the amount of effort required to get a job done. I hope you will choose well.

# Further Reading

This isn’t *everything* possible — there’s simply too much to cover. In some cases, each browser has its capabilities or utilities that can be leveraged. We explored just a few. I invite you to dig deeper to discover more ways to leverage browser DevTools for your coding projects.

*   [MDN: Console Documentation](https://developer.mozilla.org/en-US/docs/Web/API/console)
*   [Google Developers: Console API Reference](https://developers.google.com/chrome-developer-tools/docs/console-api)
*   [Google Developers: Dev Tools Tips and Tricks](https://developers.google.com/chrome-developer-tools/docs/tips-and-tricks)

# Summary

*   `console.table()` gives b*etter visualization of data.*
*   Other than `**console.log( )**`  we can have`**console.info( )**` **|** `**console.debug( )**` **|** `**console.warn( )**` **|** `**console.error( )**`
*   You can insert a `%c` variable and use various CSS styling options.
*   We have placeholder in JavaScript like `%s` `%d` `%i` `%f` `%o` that can determine **format** of the output.
*   `console.dir()` prints a JSON representation of the specified object.
*   `console.group()` can be used for *grouping messages* in the console.
*   `console.count()` logs the number of times that this particular call to `count()` has been called. This function takes an optional argument `label`.
*   `console.trace()` method displays a trace that show how the code ended up at a certain point.
*   `console.assert()` prints a statement when it is false.
*   `console.time()` can be used to track the micro-time taken for JavaScript executions.
*   Learn to see the console object as a multifaceted tool, use the right tool for the job.

# Note of Gratitude

I wanted to take this last opportunity to say thank you.

Thank you for being here! I would not be able to do what I do without  people like you  who follow along and take that leap of faith to read my post.

I hope you’ll [**join me**](https://twitter.com/tweetsbysoma) in [**my future blog post**](https://xps8zvgfv2x.typeform.com/to/AmZnFhsK) and stick around because I think we have something great here. And I hope that I can help you along in your career for many more years to come!

See you next time. Bye!



 [![Newsletter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdhccyp2ec5iz031qhtu.gif)](https://xps8zvgfv2x.typeform.com/to/AmZnFhsK)