node js server - Node js - Console - node js examples - node js tutorial




What is nodejs console?

  • nodejs has the console command similar to JavaScript, for delegating a simple code debugging.
  • The console has two specific components that are available as part of installation:

Console class:

  • This can be used to write any Node.js stream.
  • eg: console.log(), console.error() and console.warn()

Global console instance:

  • This object is a global and used without calling require('console').
  • eg: stdout and stderr.
  • This tutorial will be a best node js tutorial for beginner. Many people were confused on how to use nodejs and how to install nodejs. Our node.js getting started tutorials will provide you an in-depth knowledge on node.js framework and nodejs coding.
  • console.log ([data][, ...])

node.js documentation :

  • Many node tutorial explains this particular command.
  • It outputs data to stdout along with a newline.
  • Multiple arguments can be passed. The first value is considered as the primary message and all other are considered as substitution.
  • This nature imitates the behavior of printf command in C and C++ programs.

Sample node.js code :

var count = 10;
console.log('count: %d', count);	// Prints: count: 10, to stdout
console.log('count: ', count);// Prints: count: 10, to stdout

console.error ([data][, ...]) - node.js documentation :

  • This command is available in all node.js versions with variety of features.
  • It Prints the stderr with newline.
  • Multiple arguments can be passed. The first value is considered as the primary message and all other are considered as substitution.
  • This nature imitates the behavior of printf command in C and C++ programs.

console.timeEnd (label)- node.js documentation :

  • Stops a timer that was previously started by calling console.time()
  • It prints the result to stdout:

Sample node.js code :

console.time('ExecutionTime');
for (var i = 0; i < 100; i++) 	{
  ;
}
console.timeEnd('ExecutionTime');
 nodejs console execution

Learn Nodejs - Nodejs tutorial - nodejs console execution - Nodejs examples - Nodejs programs

  1. The nodejs command console.time('ExecutionTime'); will try to store the time of execution in the variable “ExecutionTime”.
  2. A node.js for loop is declared to loop 100 times without any major code into it.
  3. Console.timeEnd will store the time between start and end and it will be displayed to the user.

In this way, the performance of node.js code can be manipulated in real time environment as shown below:

Code Explanation :

 nodejs console code explannation

Learn Nodejs - Nodejs tutorial - nodejs console code explannation - Nodejs examples - Nodejs programs

  • As per output shown above the total time taken is displayed in the output in the form of milliseconds as Execution Time: 1716ms

console.warn ([data][, ...]) - node.js documentation :

The console.warn() function is an alternative / alias of console.Error().

console.info([data][, ...]) - node.js documentation :

The console.info() function is an alternative / alias of console.Error().

Console sample code :

Log Code Example: 
console.log('Wiki  Techy');     // gives output as Wiki Techy

Log Code Example: 
console.log('Wiki %s', 'Techy');    // like C++ stdout - formatted output

Error code example: 
console.error(new Error('Whoops, Hey Techy something U should note it down'));   // to throw the error

Warning code example: 
const name = 'Venkatesan Prabu';
console.warn(`Danger ${name}! Danger!`);    // to throw ]

Code Explanation :

 nodejs console error code

Learn Nodejs - Nodejs tutorial - nodejs console error code - Nodejs examples - Nodejs programs

  1. console.log('Wiki Techy'); -> This works like a printf statement to return back the string.
  2. console.log('Wiki %s', 'Techy'); - >This will returns the argument as stdout . instead of %s the string “Techy” will be printed.
  3. console.error(new Error('Whoops, Hey Techy something U should note it down')); -> Error messages will be thrown in this format.
  4. And Initialize the variable name with “Venkatesan Prabu” and this is appended to the warning message to display the output
  5. “warning Venkatesan Prabu! warning!“

Node JS Output :

 nodejs console error code explannation

Learn Nodejs - Nodejs tutorial - nodejs console error code explannation - Nodejs examples - Nodejs programs

  1. Console. Log command executed with plain text
  2. Console. Log command executed with stdout format
  3. Console.error command executed to display the format of error
  4. Console.warning command executed to display the format of warning.

Related Searches to node.js - Console