Node.js Lesson 7: Console Module

Node.js Lesson 7: Console Module

Node.js Lesson 7: Console Module

Hello everyone! In the last lesson, we learned about the util module. This lesson will talk about another module which is by far the most commonly used module in node.js javascript environment. We will learn all the powers we get with this mighty module that helps us in development and debugging. Let’s start

What is the console

console provides a way to print/output certain messages on to the console. It’s very useful for testing and debugging. console is a globally present module that you can use anywhere in the project without requiring it. It means something like require(‘console ‘) is not needed to use it in any file. The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. It provides us a bunch of functions that we can use to print text. Example usage:

It uses util.inspect and util.format internally. You can use this to print strings, arrays, objects, or any variable you like. It also supports string substitution just like util.format we studied in the previous lesson. Example:

Other substitution strings are %o for an object, %d for integer, %f for floating-point value, and even %c for CSS styles. Example:

Of course, it doesn’t have only .log() function. It does provide us a lot of helpful functions in accordance with the message we want to print. Example console.error() for errors, console.table() for printing tables, etc. Let’s look at all of the types in detail.

Types of functions in the console

1. console.log

We already read about this method in the examples above. This is most commonly used among all the functions to print a normal stream of the message. The first parameter is string message and the next parament can include multiple arguments for substitution.

2. console.error

We usually have two types of message streams. Apart from the normal stream, we have an error stream and that’s where console.error comes handy. The usage is pretty similar to console.log but it is used to log errors mainly.

3. console.table

This is particularly helpful when you are working with objects or tabular data with some kind of relationship. The first argument is tabular data and the second argument is property. Let us see an example:

The second argument can be used to include/exclude properties:

4. console.dir

Consider this as a sibling to console.log which takes an object and logs object properties instead of a string. console.log logs (toString()) representation of object if passed, whereas console.dir recognizes that it’s an object and treats it like that. Example:

5. console.assert

This checks if the value passed is truthy. The first argument takes a value and the second takes a message to log. This message is logged only when the value is not true. The output message always starts with ‘Assertion failed’ and it provides the formating using util.format.

6. console.count

This takes a label as the first argument and logged the number of times it is called. It maintains an internal counter and associates it with the label passed. The default label is “default” if none passed.

7. console.countReset

This function comes along with the console.count which we just read above. It clears the internal counter based on the label passed. When no label is passed, it will clear the default counter.

8. console.time & console.timeEnd

These two methods come very handy when you have to know how much time it takes to run a certain function/task. Simply add console.time() before the task starts and console.timeEnd() after the task ends. This will print out the time it took to execute that particular tasks.
They also take a label as the first argument and associate the timer with it. You can use these labels to log multiple time logs but remember that they should be unique. .time() starts the timer and .timeEnd() ends the timer. They both should be used together. Example:

9. console.timeLog

This also works with the above two methods but is optional. You can use this to log elapsed time after the timer starts.

10. console.debug

The console.debug() function is an alias for console.log(). Although they can be used interchangeably, it is preferred to use console.log.

11. console.clear

This simply clears old logs on the console. If some logs are present, it will clear them. Otherwise, this function does nothing.

12. console.trace

Logs the stack trace and helps in debugging.

13. console.warn

The console.warn() function is an alias for console.error().

Takeaways

  • the console is one of the most helpful modules that you will need more often
  • console.log and console.error should be used for the normal stream and error stream of messages respectively.
  • console.table help us log tables in a beautiful format and thus helps us in debugging.

I hope this was helpful for you in understanding the console module better. You can find this lesson coding in our repository.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

30.01.2024

Nest.js and AWS Lambda for Serverless Microservices

By combining Nest.js and AWS Lambda, developers can harness the benefits of serverless computing to build flexible, resilient, and highly scalable ...

16.05.2023

Interoperability between Ethereum, Binance Smart Chain, and other blockchain platforms using Node.js

In this article, I will deeply into the importance of interoperability in the blockchain sphere and present use cases that support this perspective. ...

25.04.2023

Anime.js to MP4 and GIF with Node.js and FFMPEG

While canvas animation is often considered the simpler approach for exporting animations to video and GIF formats, it can also limit the range of ...

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy