09Oct
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:

console.log("This is a message to print");

// This is a message to print

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:

console.log("2 + 2 equals %d", 4);

// 2 + 2 equals 4

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

console.log("%c this is red color log", "color: red");

// prints "this is red color" log but in red color

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.

console.log("%d + %d is equal to %d", 2, 5, 7);

// 2 + 5 is equal to 7

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.

console.error('error', 404);

// error 404

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:

console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);

This logs:

| (index) | a   | b   |
|---------|-----|-----|
|    0    |  1  | 'Y' |
|    1    | 'Z' |  2  |

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

console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['b']);

This logs:
| (index) | b   |
|---------|-----|
|    0    | 'Y' |
|    1    |  2  |

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:

console.dir([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);

// [ { a: 1, b: 'Y' }, { a: 'Z', b: 2 } ]

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.

console.assert(true, 'truthy value');
// logs nothing

console.assert(false, 'whoopsy, this is false');
// Assertion failed: whoopsy, this is false

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.

console.count();
console.count();
console.count();

console.count('wow');
console.count('wow');

// default: 1
// default: 2
// default: 3
// wow: 1
// wow: 2

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.

console.count('wow');
console.count('wow');
console.count('wow');

console.countReset('wow); //reset counter
console.count('wow');

console.count('wow');

// wow: 1
// wow: 2
// wow: 3

// wow: 1
// wow: 2

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:

// timer starts
console.time();

// running a loop to mimic a long task
let a = 0;
while(a < 100000){
    a++;
}

// timer stops
console.timeEnd();

// default: 1.843ms

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.

// example for Nodejs docs

console.time('process');
const value = expensiveProcess1(); // Returns 42
console.timeLog('process', value);
// Prints "process: 365.227ms 42".
doExpensiveProcess2(value);
console.timeEnd('process');

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.

console.count();
console.count();
console.count();

console.clear();

// logs nothing

12. console.trace

Logs the stack trace and helps in debugging.

console.trace('What happened when I logged this message');

// Trace: What happened when I logged this message
//     at Object.<anonymous> (/Users/mdshadmirza/personal/BlogsByShad/test.js:33:9)
//     at Module._compile (internal/modules/cjs/loader.js:1201:30)
//     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
//     at Module.load (internal/modules/cjs/loader.js:1050:32)
//     at Function.Module._load (internal/modules/cjs/loader.js:938:14)
//     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
//     at internal/main/run_main_module.js:17:47

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.

JavaScript lover working on React Native and committed to simplifying code for beginners. Apart from coding and blogging, I like spending my time sketching or writing with a cup of tea.

Code Style

The main things that I would like to draw your attention to:
1) The names variables and methods should be clear and concise, don’t skimp on characters.
2) Methods should not exceed 30-40 lines, they are intended for solving a single specific task, and poor methods do everything.