29

https://levelup.gitconnected.com/moving-beyond-console-log-8-console-methods-you...

 4 years ago
source link: https://t.co/HSVgSHEPVV
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.

Moving beyond console.log() β€” 8 Console Methods You Should Use When Debugging JavaScript and Node

Moving beyond console.log and learn the console functions you have never used for debugging!

Image for post
Image for post

The Console API

Every JavaScript developer has used console.log(β€˜text’). The console module is one of the most common utilities in JavaScript, and the API implemented in Node:

provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

This is the definition written in the Node.js documentation page for the Console module πŸ˜…. However, beginners are prone to consult online tutorials instead of reading the documentation while starting with new technologies, missing the chance to learn how to properly use this new tool to 100% of its potential.

When talking about the Console API, newbies usually use only some functions like πŸ‘Œconsole.log(), ⚠️ console.warn(), or ❌ console.error()to debug their application, while often there are many other methods which can perfectly implement our requirements and improve debugging efficiency.

This article is made to expose some of the most interesting console methods with related examples that I use while teaching at Codeworks. So let’s see a list of the 8 best functions from the Console module!

All the following methods are available in the global instance console, so it is not necessary to require the console module.

1) console.assert βœ…

The console.assert function is used to test if the passed argument is a truthy or falsey value. In the case that the passed value is falsey, the function logs the extra arguments passed after the first one, otherwise, the code execution proceeds without any log.

Image for post
Image for post
Both cases of a truthy or falsy assertion.

The assert method is particularly useful whenever you want to check the existence of values while keeping the console clean (avoid logging long list of properties, etc.).

2) console.count and console.countReset πŸ’―

These two methods are used to set and clear a counter of how many times a particular string gets logged in the console:

Image for post
Image for post
Count and reset the log occurrences for the β€œHello” string.

3) console.group and console.groupEnd 🎳

.group and .groupEnd create and end a group of logs in your console. You can pass a label as the first argument of .group() to describe what it is concerned about:

Image for post
Image for post
Three groups to describe family roles.

4) console.table πŸ“‹

This particular method is incredibly useful to describe an object or array content in a human-friendly table:

Image for post
Image for post
Table for a list of user objects.

console.table makes it easier for the inspection and logging of nested and complex arrays/objects.

5) console.time and console.timeEnd ⏱

In the case that you want to check the performance of your code in execution time, and to solve it you create a start timestamp with the Date API and use it to compute the difference after your code execution? Something like this:

Image for post
Image for post

Well, using the time and timeEnd functions, it is not necessary to do this trick. You can create your timing report simply by doing:

Image for post
Image for post
As you can see, console.time also returns a more accurate result.

Summary

With only 3 minutes of your time, you now have a larger scope of some of the wonderful tools available in the Console API. Integrate them with your debugging habits and your development speed will increase exponentially!

See you with the next chapter of Learning Node.js! πŸš€πŸŽ‰


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK