We now have a youtube channel. Subscribe!

Node.js | Console

Node.js | Console


Hello folks! welcome back to a new edition of our tutorial on Node.js. In this section of our tutorial on Node.js, we will be studying about Node.js Console.

Node.js console is a global object used to print different levels of messages to stdout and stderr. There are built-in methods to be used for printing informational, warning, and error messages.

It is used in synchronous manner when the destination is a file or a terminal and in an asynchronous manner when the destination is a pipe.


Node.js Console Methods

Below is the list of methods available with the console global object -

Sr.No.Method & Description
1

console.log([data][, ...])

Prints to stdout with newline. This function can take multiple arguments in a printf()-like way.

2

console.info([data][, ...])

Prints to stdout with newline. This function can take multiple arguments in a printf()-like way.

3

console.error([data][, ...])

Prints to stderr with newline. This function can take multiple arguments in a printf()-like way.

4

console.warn([data][, ...])

Prints to stderr with newline. This function can take multiple arguments in a printf()-like way

5

console.dir(obj[, options])

Uses util.inspect on obj and prints resulting string to stdout.

6

console.time(label)

Mark a time.

7

console.timeEnd(label)

Finish timer, record output.

8

console.trace(message[, ...])

Print to stderr 'Trace :', followed by the formatted message and stack trace to the current position.

9

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

Similar to assert.ok(), but the error message is formatted as util.format(message...).


Example

Create a js file, main.js using the following code -

console.info("Program Started");

var counter = 10;
console.log("Counter: %d", counter);

console.time("Getting data");
//
// Do some processing here...
// 
console.timeEnd('Getting data');

console.info("Program Ended")

Now run the main.js to see the result -

node main.js

Output

Verify the output.

Program Started
Counter: 10
Getting data: 0ms
Program Ended


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about Node.js Process Module.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain