Node.js console.log and beyond

If you’ve ever written some javascript code you are probably familiar with the console.log function. I often use it for debugging reasons. But did you know that the Console module contains a lot of other different functions among the well known console.log, console.error and console.warn?

console.assert

Let’s have a look at the console.assert right now. To this function you pass the conditional expression as the 1st param and the output, that should be printed out to the console if the expression is falsy.

// these messages will not be printed as the 
// condition expression if truthy
console.assert(true, 'Hello, my dear.')
console.assert(1, 'Hello, my dear.')
console.assert('hi', 'Hello, my dear.')

// these messages will be printed
console.assert(false, 'Hello, my dear.')
console.assert(0, 'Hello, my dear.')
console.assert('', 'Hello, my dear.')

// the usage in real code may be following
const x = 5;
console.assert(x < 10, 'Given value is too small');

You may avoid logging the messages you don’t need and your log output will be cleaner.

console.table

Have you ever printed out an array of JSON data to the console? I have. The reading such output is not very user friendly though. Now it’s the console.tables turn. Look at the example below, isn’t it fantastic?

const myPets = [{name: "Tweety", type: "Bird", age: 2}, {name: "Puck", type: "Dog", age: 1}];

console.log(myPets);
// prints:
// [ { name: 'Tweety', type: 'Bird', age: 2 },
//   { name: 'Puck', type: 'Dog', age: 1 } ]

console.table(myPets);
// prints:
// ┌─────────┬──────────┬────────┬─────┐
// │ (index) │   name   │  type  │ age │
// ├─────────┼──────────┼────────┼─────┤
// │    0    │ 'Tweety' │ 'Bird' │  2  │
// │    1    │  'Puck'  │ 'Dog'  │  1  │
// └─────────┴──────────┴────────┴─────┘

console.time and console.timeEnd

Whenever I needed to measure the time in my code I did it this way.

  1. create const start to keep starting timestamp const start = Date.now();
  2. write the code that should spend some machine time
  3. compute the spent time by doing const diff = Date.now() - start; that gives me the duration in ms
  4. print out the duration using console.log

But wait, there are two console functions called time and timeEnd. If you just need to print out the duration result to stdout you may use the example code below. Unfortunately when you need to keep the duration if some variable and process it in further code you must do it yourself as mentioned in steps 1.-4. above.

// you may set the label which will be used for timeEnd and printed out later
console.time('My observed duration');
setTimeout(() => {
    // will print out: "My observed duration: 1502.4257852ms"
    console.timeEnd('My observed duration')
}, 1500);

console.count

Sometimes it is fair enough to check how many times the function/method is called. It may be very useful when dealing with recursive functions etc. to get known what is going on inside.

For this purpose the console.count may be used. You set the label and each time it is called, this label and count information is printed to stdout. If you need to reset the counter, just call the console.countReset([label]).

console.count('test');
// test: 1
console.count('test');
// test: 2
console.count('test');
// test: 2

console.countReset('test');

console.count('test');
// test: 1

summary

When prototyping or doing the basic code debugging, console function are very handy. Check out the official console docs for other functions that were not covered by this post. Happy coding!

Close Menu