Learning Node.js: 5 Tips to Use the Console API8th June 2019
The console functions you have never used for debugging!
The Console API
At least, this is the definition written in the Node.js documentation page for the Console module 😅. However, beginners are most prone to consult online tutorials than reading the documentation while starting with new technologies, missing the chance to learn how to properly use this new tool at 100% of its potential.
And talking about the Console API, newbies usually use only some functions like 👌console.log(), ⚠️ console.warn() or ❌ console.error() to debug their application, while ofter there are many others methods which can perfectly implement our requirements and improve the debugging efficiency.
This article is made to expose some of the most interesting of them with a related example, so let’s immediately start with a list of 8 best functions from the Console module!
All the following methods are available in the global instance console, so is not necessary to require the console module.
1) Console.assert ✅
The Console.assert function is used to test if the passed argument is truthy or falsy value. In case the passed value is falsy, the function logs the extra arguments passed after the first one, otherwise, the code execution proceeds without any log.
The assert method is particularly useful whenever you want to check the existence of values keeping the console clean (avoid logging long list of properties, etc…).
2) Console.count & Console.countReset 💯
These two methods are respectively used to set and clear a counter of how many times a label gets logged on the console:
3) Console.group & Console.groupEnd 🎳
.group and .groupEnd create and close a group of logs on your console, and you can pass a label as the first argument of .group() to describe what is concerned about:
4) Console.table 📋
This particular method is incredibly useful to describe an object or array content through a human-friendly table:
Console.table makes easier the inspection and logging of nested and complex arrays/objects.
5) Console.time & Console.timeEnd ⏱
How often do you want to check the performance of your code in time execution, so you create a start timestamp with the Date API and use it to compute the difference after your code execution? Something like this:
Well, using the time and timeEnd functions is not necessary to do this trick, you can create your custom report simply doing so:
With only 3 minutes of your time, you now have a panoramic on some 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! 🚀🎉
Comments, shares and discussion about the topic are always well accepted and I’ll be glad to answer any of your questions!
Feel free to contact me on Linkedin:https://www.linkedin.com/in/marcoantonioghiani/
Learning Node.js: 5 Tips to Use the Console API was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.