30 Days of Node
Inside this article!
- Introduction
- Console and its types
- New Console
- Clear Console
- Count console
- Count reset console
- Console error
- Console time
- Console Warn
- Summary
- Repository
Introduction to console
The console
module provides us with debugging console similar to javascript console mechanism web browsers provide us. It exports two components :
- console class : It includes methods such as
console.log()
,console.warn()
,console.error()
which we can use to write to node.js streams. - Global console instance : This method is configured to write on
process.stderr
,process.stdout()
and it can be used without exclusively calling the modulerequire('console')
.
console.log() and all its variations
console.log()
is used to print to stdout
with a newline. We can pass multiple arguments. Let’s see the different variations of console.log
in the example given below :
// console.log().js
// using ' ' to print
console.log('1: hello world!');
// using " " to print
console.log("2: This will also work!");
const str = 'nodejsera';
const val = 25;
// printing a string
console.log(str);
// printing a variable and replacing the
// value of variable in place of %d
console.log('4: Value of val is: %d', val);
// replacing a string in place of %s
console.log('5: %s', 'this will be printed after 5');
// concatinating in console
console.log("6: str = " + str);
The Output is:
1: hello world!
2: This will also work!
nodejsera
4: Value of val is: 25
5: this will be printed after 5
6: str = nodejsera
Create a new console
Creating a new console where stdout</