28

Advanced console.log Tips & Tricks - Nielsen-Tel-Aviv-tech-blog - Medium

 4 years ago
source link: https://medium.com/nmc-techblog/advanced-console-log-tips-tricks-fa3762930bca
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Advanced console.log Tips & Tricks

The console is the built-in debugger of the browser. Many developers use console.log() all the time to print messages and debug problems in their code. But this tool has a lot more to offer than you may have ever realized!

Intro — The Console Object

Let’s try something simple and run:

console.log(console)
Image for post
Image for post
Image for post
Image for post

As we can see, the console object has a lot more to offer than the good ol’ console.log()function.

The truth is that there is probably a better way to log anything you need. All it takes is to know the alternative options you have and to get yourself used to them.

In this article, I’m going to introduce some of the more useful functionalities of the console object that I’ve adopted during my time as a web developer. After taking a look, you can judge for yourself if console.log() should remain your only go-to option for print debugging.

Let’s create a test object to use as an example:

Image for post
Image for post

And that’s how it looks when we call console.log() with our object:

Image for post
Image for post

console.dir()

While console.log() prints the object in its string representation, console.dir() recognizes the object as an object and prints it’s properties in the form of a clean expandable list:

Image for post
Image for post

console.assert()

With console.assert(), we can decide only to log something if a condition is false and save some console space by avoiding unnecessary message printing:

Image for post
Image for post

console.count()

Instead of incrementing a counter manually, we can use console.count() to do the job for us! We can provide it with a label (“Pets” in this example), and the counter value will increment each time console.count(“Pets”) is called:

Image for post
Image for post

console.table()

My personal favorite! Use console.table() to print a visually nice table representation of an object with labeled rows for each the objects’ properties:

Image for post
Image for post

console.time() / timeLog() / timeEnd()

A popular one. Use console.time() to start a timer. Then, with each call to console.timeLog(), the time elapsed since the timer started will be printed.
When done, call console.timeEnd() to print the total amount of time:

Image for post
Image for post

console.trace()

Another very useful one! When debugging deeply nested objects or functions, we may want to print the stack trace of the code. Call console.trace() from within the function you want at the top of the call stack, to see the exact places in the code that called it:

Image for post
Image for post

console.group() / groupEnd()

It’s possible to group messages to avoid spam and mess in the console, like so:

Image for post
Image for post

Extra tip: use console.groupCollapsed() instead of console.group() and the group will be collapsed by default. This also makes things easier on the eyes.

Bonus: CSS styling the console

Just in case you want to flex your new godly logging abilities, use “%c” before a string and pass CSS styles as another argument:

Image for post
Image for post

Now you can practically use the console to paint anything! 🎨

Summary

Sometimes we don’t realize we can make our lives as developers a little bit easier just by adapting new small habits, like being a console pro 😎.

I hope you find this information useful.
Happy debugging!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK