How to use console in node.js

How to use console in node.js

In this article, we'll learn how to use most methods available in the nodeJS console class more effectively.

In this article, we'll learn how to use most methods available in the nodeJS console class more effectively.

Introduction

Table of Contents

To demonstrate, I’ll use Chrome browser version 70.0.3538.77 and nodeJS version 8.11.** 3**.

Okay then. Let’s begin.

console.log, console.info and console.debug
console.log(string, substitution)

While the famed console.log method really needs no introduction, you’ll be glad to know that the console.info and console.debug methods are identical to console.log in their operation.

You can use console.debug in the Firefox browser console by default but to use it in Chrome, you’ll have to set the log level to verbose like this.

The console.log method prints to standard out, whether this be the terminal or browser console. It outputs strings by default but can be used in conjuction with template strings to modify what it returns.

Here’s how it works:

The arguments in the template string are passed to util.format which then processes the arguments by replacing each substitution token with the respective converted value. The supported substitution tokens are:

%s

const msg = `Using the console class`;
console.log('%s', msg); // Using the console class
console.log(msg); // Using the console class

%s is the default substitution pattern.

%d, %f, %i, %o

const circle = (radius = 1) => {
  const profile = {};
  const pi = 22/7;
  profile.diameter = 2_pi_radius;
  profile.circumference = pi_radius_2;
  profile.area = pi_radius^2;
  profile.volume = 4/3_pi_radius^3;

  console.log('This circle has a radius of: %d cm', radius);
  console.log('This circle has a circumference of: %f cm', profile.diameter);
  console.log('This circle has an area of: %i cm^2', profile.area);
  console.log('The profile of this cirlce is: %o', profile);
  console.log('Diameter %d, Area: %f, Circumference %i', profile.diameter, profile.area, profile.circumference)
}

circle();

This is what happens:

%d will be substituted by a digit (integer or float). %f will be replaced by a float value. %i will be replaced by an integer. %o will be replaced by an Object.

%o is especially handy because we don’t have to use JSON.stringify to expand our object because it shows all the object’s properties by default.

Note that you can use as many token substitutions as you like. They’ll just be replaced the same order as the arguments you pass.

%c

This substitution token applies css styles to the subsituted text.

console.log('LOG LEVEL: %c OK', 'color: green; font-weight: normal');
console.log('LOG LEVEL: %c PRIORITY', 'color: blue; font-weight: medium');

console.log('LOG LEVEL: %c WARN', 'color: red; font-weight: bold');
console.log('ERROR HERE');

Here it is in action.

Above, we note that the text we pass to console.log after the %c substitution token is affected by the styles, but the text before is left as is without styling.

console.table

The first argument passed to it is the data to be returned in the form of a table. The second is an array of selected columns to be displayed.

console.table(tabularData, [properties])

This method will print the input passed to it formatted as a table then log the input object after the table representation.

Arrays

If an array is passed to it as data, each element in the array will be a row in the table.

const books = ['The Silmarillion', 'The Hobbit', 'Unfinished Tales'];
console.table(books);

With a simple array with a depth of 1, the first column in the table has the heading index. Under the index header in the first column are the array indexes and the items in the array are listed in the second column under the value header.

This is what happens for a nested array.

const authorsAndBooks = [['Tolkien', 'Lord of The Rings'],['Rutger', 'Utopia For Realists'], ['Sinek', 'Leaders Eat Last'], ['Eyal', 'Habit']];
console.table(authorsAndBooks);

Objects

For objects with a depth of 1, the object keys will be listed under the index header and the values in the object under the second column header.

const inventory = { apples: 200, mangoes: 50, avocados: 300, kiwis: 50 };
console.table(inventory);

For nested objects,

const forexConverter = { asia: { rupee: 1.39, renminbi: 14.59 , ringgit: 24.26 }, africa: { rand: 6.49, nakfa: 6.7 , kwanza:0.33 }, europe: { swissfranc: 101.60, gbp: 130, euro: 115.73 } };
console.table(forexConverter);

Some more nested objects,

const workoutLog = { Monday: { push: 'Incline Bench Press', pull: 'Deadlift'}, Wednesday: { push: 'Weighted Dips', pull: 'Barbell Rows'}};
console.table(workoutLog);

Here, we specify that we only want to see data under the column push.

console.table(workoutLog, 'push');

To sort the data under a column, just click the column header.

Pretty handy, eh?

Try passing console.table an object with some values as arrays!## console.dir

The first argument passed to this function is the object to be logged while the second is an object containing options that will define how the resulting output is fomatted or what properties in the object will be shown.

What’s returned is an object formatted by node’s util.inspect function.

Nested or child objects within the input object are expandable under disclosure triangles.

console.dir(object, options);
// where options = { showHidden: true ... }

Let’s see this in action.

const user = {
  details: {
    name: {
      firstName: 'Immanuel',
      lastName: 'Kant'
    },
    height: `1.83m"`,
    weight: '90kg',
    age: '80',
    occupation: 'Philosopher',
    nationality: 'German',
    books: [
      {
        name: 'Critique of Pure Reason',
        pub: '1781',
      },
      {
        name: 'Critique of Judgement',
        pub: '1790',
      },
      {
        name: 'Critique of Practical Reason',
        pub: '1788',
      },
      {
        name: 'Perpetual Peace',
        pub: '1795',
      },
    ],
    death: '1804'
  }
}

console.dir(user);

Here it is in the Chrome console.

console.dirxml

This function will render an interactive tree of the XML/HTML it is passed. It defaults to a Javascript object if it’s not possible to render the node tree.

console.dirxml(object|nodeList);

Much like console.*_dir, *_the rendered tree can be expanded through clicking disclosure triangles within which you can see child nodes.

It’s output is similar to that which we find under the Elements tab in the browser.

This is how it looks when we pass in some HTML from a Wikipedia page.

Let’s pass in some HTML from a page on this website.

This is how it looks when we pass in an object.

Try passing console.table an object with some values as arrays!## console.assert

The first argument passed to the function is a value to test as truthy. All other arguments passed are considered messages to be printed out if the value passed is not evaluated as truthy.

The Node REPL will throw an error halting execution of subsequent code.

console.assert(value, [...messages])

Here’s a basic example:

console.assert(false, 'Assertion failed'); // Assertion failed

Now, let’s have some fun. We’ll build a mini testing framework using console.assert

const sum = (a = 0, b = 0) => Number(a) + Number(b);

function test(functionName, actualFunctionResult, expected) {
  const actual = actualFunctionResult;
  const pass = actual === expected;
  console.assert(pass, `Assertion failed for ${functionName}`);
  return `Test passed ${actual} === ${expected}`;
}

console.log(test('sum', sum(1,1), 2)); // Test passed 2 === 2
console.log(test('sum', sum(), 0)); // Test passed 0 === 0
console.log(test('sum', sum, 2)); // Assertion failed for sum
console.log(test('sum', sum(3,3), 4)); // Assertion failed for sum

Run the above in your node REPL or browser console to see what happens.

console.error & console.warn

These two are essentially identical. They will both print whatever string is passed to them.

However, console.warn prints out a triangle warn symbol before the message passed while console.error prints out a danger symbol before the message passed.

console.error(string, substitution);
console.warn(string, substitution);

Let’s note that string substitution can be applied in the same way as the console.log method.

Here’s a mini logging function using console.error.

const sum = (a = 0, b = 0) => Number(a) + Number(b);

function otherTest(actualFunctionResult, expected) {
  if (actualFunctionResult !== expected) {
    console.error(new Error(`Test failed ${actualFunctionResult} !== ${expected}`));
  } else {
    // pass
  }
}

otherTest(sum(1,1), 3);

console.trace(label)

This console method will prints the string Trace: followed by the label passed to the function then the stack trace to the current position of the function.

function getCapital(country) {
  const capitalMap = {
    belarus: 'minsk', australia: 'canberra', egypt: 'cairo', georgia: 'tblisi', latvia: 'riga', samoa: 'apia'
  };
  console.trace('Start trace here');
  return Object.keys(capitalMap).find(item => item === country) ? capitalMap[country] : undefined;
}

console.log(getCapital('belarus'));
console.log(getCapital('accra'));

console.count(label)

Count will begin and increment a counter of name label** .**

Let’s build a word counter to see how it works.

const getOccurences = (word = 'foolish') => {
  const phrase = `Oh me! Oh life! of the questions of these recurring, Of the endless trains of the faithless, of cities fill’d with the foolish, Of myself forever reproaching myself, for who more foolish than I, and who more faithless?`;

  let count = 0;
  const wordsFromPhraseArray = phrase.replace(/[,.!?]/igm, '').split(' ');
  wordsFromPhraseArray.forEach((element, idx) => {
    if (element === word) {
      count ++;
      console.count(word);
    }
  });
  return count;
}

getOccurences();

Here, we see that the word foolish was logged twice. Once for each appearance of the word in the phrase.

We could use this as a handy method to see how many times a function was called or how many times a line in our code executed.

console.countReset(label)

As the name suggests, this resets a counter having a label set by the console.count method.

const getOccurences = (word = 'foolish') => {
  const phrase = `Oh me! Oh life! of the questions of these recurring, Of the endless trains of the faithless, of cities fill’d with the foolish, Of myself forever reproaching myself, for who more foolish than I, and who more faithless?`;

  let count = 0;
  const wordsFromPhraseArray = phrase.replace(/[,.!?]/igm, '').split(' ');
  wordsFromPhraseArray.forEach((element, idx) => {
    if (element === word) {
      count ++;
      console.count(word);
      console.countReset(word);
    }
  });
  return count;
}

getOccurences();

We can see that our getOccurences function returns 2 because there are indeed two occurences of the word foolish in the phrase but since our counter is reset at every match, it logs foolish: 1twice.

console.time(label) and console.timeEnd(label)

The console.time function starts a timer with the label supplied as an argument to the function, while the console.*_timeEnd *_function stops a timer with the **label supplied as an argument to the function.

console.time('<timer-label>');
console.timeEnd('<timer-label'>);

We can use it to figure out how much time it took to run an operation by passing in the same label name to both functions.

const users = ['Vivaldi', 'Beethoven', 'Ludovico'];

const loop = (array) => {
  array.forEach((element, idx) => {
    console.log(element);
  })
}

const timer = () => {
  console.time('timerLabel');
  loop(users);
  console.timeEnd('timerLabel');
}

timer();

We can see the timer label displayed against time value after the timer is stopped.

It took the loop function 0.6909ms to finish looping through the array.

Conclusion

At last, we’ve come to the end of this tutorial. I hope you’ve enjoyed it.

I’ve left out the non standard uses of the console class like console.profile, console.profileEnd and console.timeLog , but feel free to experiment with them and let me know how it goes.

Learn More

The Complete Node.js Developer Course (2nd Edition)

Learn and Understand NodeJS

Node JS: Advanced Concepts

GraphQL: Learning GraphQL with Node.Js

Angular (Angular 2+) & NodeJS - The MEAN Stack Guide

Beginner Full Stack Web Development: HTML, CSS, React & Node

Node with React: Fullstack Web Development

MERN Stack Front To Back: Full Stack React, Redux & Node.js

Top 7 Most Popular Node.js Frameworks You Should Know

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser.

One of the main advantages of Node is that it enables developers to use JavaScript on both the front-end and the back-end of an application. This not only makes the source code of any app cleaner and more consistent, but it significantly speeds up app development too, as developers only need to use one language.

Node is fast, scalable, and easy to get started with. Its default package manager is npm, which means it also sports the largest ecosystem of open-source libraries. Node is used by companies such as NASA, Uber, Netflix, and Walmart.

But Node doesn't come alone. It comes with a plethora of frameworks. A Node framework can be pictured as the external scaffolding that you can build your app in. These frameworks are built on top of Node and extend the technology's functionality, mostly by making apps easier to prototype and develop, while also making them faster and more scalable.

Below are 7of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Express

With over 43,000 GitHub stars, Express is the most popular Node framework. It brands itself as a fast, unopinionated, and minimalist framework. Express acts as middleware: it helps set up and configure routes to send and receive requests between the front-end and the database of an app.

Express provides lightweight, powerful tools for HTTP servers. It's a great framework for single-page apps, websites, hybrids, or public HTTP APIs. It supports over fourteen different template engines, so developers aren't forced into any specific ORM.

Meteor

Meteor is a full-stack JavaScript platform. It allows developers to build real-time web apps, i.e. apps where code changes are pushed to all browsers and devices in real-time. Additionally, servers send data over the wire, instead of HTML. The client renders the data.

The project has over 41,000 GitHub stars and is built to power large projects. Meteor is used by companies such as Mazda, Honeywell, Qualcomm, and IKEA. It has excellent documentation and a strong community behind it.

Koa

Koa is built by the same team that built Express. It uses ES6 methods that allow developers to work without callbacks. Developers also have more control over error-handling. Koa has no middleware within its core, which means that developers have more control over configuration, but which means that traditional Node middleware (e.g. req, res, next) won't work with Koa.

Koa already has over 26,000 GitHub stars. The Express developers built Koa because they wanted a lighter framework that was more expressive and more robust than Express. You can find out more about the differences between Koa and Express here.

Sails

Sails is a real-time, MVC framework for Node that's built on Express. It supports auto-generated REST APIs and comes with an easy WebSocket integration.

The project has over 20,000 stars on GitHub and is compatible with almost all databases (MySQL, MongoDB, PostgreSQL, Redis). It's also compatible with most front-end technologies (Angular, iOS, Android, React, and even Windows Phone).

Nest

Nest has over 15,000 GitHub stars. It uses progressive JavaScript and is built with TypeScript, which means it comes with strong typing. It combines elements of object-oriented programming, functional programming, and functional reactive programming.

Nest is packaged in such a way it serves as a complete development kit for writing enterprise-level apps. The framework uses Express, but is compatible with a wide range of other libraries.

LoopBack

LoopBack is a framework that allows developers to quickly create REST APIs. It has an easy-to-use CLI wizard and allows developers to create models either on their schema or dynamically. It also has a built-in API explorer.

LoopBack has over 12,000 GitHub stars and is used by companies such as GoDaddy, Symantec, and the Bank of America. It's compatible with many REST services and a wide variety of databases (MongoDB, Oracle, MySQL, PostgreSQL).

Hapi

Similar to Express, hapi serves data by intermediating between server-side and client-side. As such, it's can serve as a substitute for Express. Hapi allows developers to focus on writing reusable app logic in a modular and prescriptive fashion.

The project has over 11,000 GitHub stars. It has built-in support for input validation, caching, authentication, and more. Hapi was originally developed to handle all of Walmart's mobile traffic during Black Friday.

Difference between AngularJS, React, Ember, Backbone, and Node.js.

The most common thing between all of them is that they are Single Page Apps. The SPA is a single page where much of the information remains the same and only some piece of data gets modified when you click on other categories/option.

Node.js Tutorial for Beginners | Node.js Crash Course | Node.js Certification Training

This courseis designed for professionals who aspire to be application developers and gain expertise in building real-time, highly-scalable applications in Node.js. The following professionals can go for this course :

Why learn Node.js?

Node.js uses JavaScript - a language known to millions of developers worldwide - thus giving it a much lower learning curve even for complete beginners. Using Node.js you can build simple Command Line programs or complex enterprise level web applications with equal ease. Node.js is an event-driven, server-side, asynchronous development platform with lightning speed execution. Node.js helps you to code the most complex functionalities in just a few lines of code...

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

The Complete Node.js Developer Course (3rd Edition)

Angular & NodeJS - The MEAN Stack Guide

NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

Docker for Node.js Projects From a Docker Captain

Intro To MySQL With Node.js - Learn To Use MySQL with Node!

Node.js Absolute Beginners Guide - Learn Node From Scratch

React Node FullStack - Social Network from Scratch to Deploy

Selenium WebDriver - JavaScript nodeJS webdriver IO & more!

Complete Next.js with React & Node - Beautiful Portfolio App

Build a Blockchain & Cryptocurrency | Full-Stack Edition