NodeJS Console Methods – 8 Methods to Know

Node.js includes various global objects used to perform different operations. Global objects are global in nature, they can be used anywhere directly. One of the widely used global objects is the console object which is used literally in every Node.js project.

NodeJS Console Methods

A console object is the global Node.js object used to print different types of values like information, error, warning, object’s properties, time, etc. 

There are different methods of the console object in Node.js that we can use according to the application’s needs. 

  1. console.log()
  2. console.info()
  3. console.error()
  4. console.warn()
  5. console.dir()
  6. console.time()
  7. console.timeEnd()
  8. console.assert()

1. console.log()

This method is used to print a value in the console.

Syntax:

console.log(msg)

Where msg is the value you want to show in the console.

Example:

console.log("Log: Hello World!")

Output:

Log: Hello World!

2. console.info()

This method is used to print informational messages in the console. The difference between console.log and console.info is that some browsers display the “i” icon next to the information messages like firefox.

Syntax:

console.info(msg)

Where msg is the information you want to display in the console.

Example:

console.info("Info: Hello World!")

Output:

Info: Hello World!

3. console.error()

This method is used to print errors in the console.

Syntax:

console.error(err)

Where err is the error you want to display in the console.

Example:

console.error("Error: Hello World!")

Output:

Error: Hello World!

4. console.warn()

This method is used to print the warning in the console.

Syntax:

console.warn(warning)

Where the warning is the warning you want to display in the console.

Example:

console.warn("Warning: Hello World!")

Output:

Warning: Hello World!

5. console.dir()

This method is used to print the properties of a JavaScript object in the console.

We can also use console.log() for this but the difference between them is the console.dir() makes a copy of an object before logging whereas console.log() passes that object’s reference.

Syntax:

console.dir(obj)

Where obj is the object which properties you want to display in the console.

Example:

const obj = {
    firstLetter: "Hello",
    lastLetter: "World!",
}

console.dir(obj)

Output:

{ firstLetter: 'Hello', lastLetter: 'World!' }

6. console.time()

This method starts a timer with the name specified as the augment.

Syntax:

console.time(msg)

7. console.timeEnd()

This method is used to stop the timer start by console.time() and show the recoded time from the start to the end in the console.

Syntax:

console.timeEnd(msg)

Where msg is the value for which you stop the timer and display it in the console with time.

Example:

console.time("Time")
console.timeEnd("Time") 

Output:

Time: 0.943ms

8. console.assert()

This method performs assertion and if the assertion is false then it throws the error message in the console.

Syntax:

console.assert(assertion, msg)

Where the msg can be a value you want to display in the console when the assertion is false.

Example:

console.assert((4 == 2), "4 is not equal to 2")

Output:

Assertion failed: 4 is not equal to 2

Summary

The console is one of the most important objects in JavaScript which can be directly called anywhere, it is a global object that comes with various methods used to perform different operations. It is used to show information, error, and object properties in the console. It is also used to trace the time in the console, print stack trace, etc. We hope this tutorial helped you understand the NodeJS console.

Reference

https://developer.mozilla.org/en-US/docs/Web/API/console

https://stackoverflow.com/questions/11954152/whats-the-difference-between-console-dir-and-console-log

Aditya Gupta
Aditya Gupta
Articles: 109